How to clip?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
t.hoepfner
Posts: 18
Joined: 2006-12-18T14:41:21-07:00

How to clip?

Post by t.hoepfner »

Hi,

I have several images with clipping paths (originating from Photoshop CS2; JPG, also tried TIFF and PSD; CMYK) and I'm trying to figure out, how to remove everything outside of the clipping path.

From the docs, I would have expected that e.g.

convert src.jpg -clip -trim dest.jpg

would do the trick, but this seems to do nothing (also no errors reported).

I also tried the example from the docs with

convert -clip -negate src.jpg dest.jpg

but this also does nothing.

Played around with -clip-mask and -clip-path. But either with the same result or an error.

Using "identify -verbose src.jpg" I can verify, there's a clipping path in there. Also copy and pasted the svg data to a new file and verified the path with Illustrator. Looks OK.

Help please,

Timo

PS: Version 6.3.0-7 on MacOS X 10.4.8, installed via DarwinPorts/MacPorts
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

There was a bug related to clipping fixed recently. The patch is available in the latest ImageMagick release 6.3.1-2. If 6.3.1-2 fails, post a URL to your image and we will investigate further.
t.hoepfner
Posts: 18
Joined: 2006-12-18T14:41:21-07:00

Post by t.hoepfner »

Thanks for your help, I've built 6.3.1-2 with the same settings and libs as 6.3.0. No change related to the clipping stuff for me.

Also tried the latest Windows binary on a different machine with the same results. Are the windows binaries compiled with support for XML?

One thing to note: My previous 6.3.0 build would often complain about "no clip path defined" for TIFFs, while I didn't get errors for JPGs. With 6.3.1 it's exactly the other way around. In this regard, the Windows binaries from imagemagick.org and my self-compiled 6.3.1-2 for MacOS X behave the same.

I'll prepare a sample image and post the URL.

Timo
t.hoepfner
Posts: 18
Joined: 2006-12-18T14:41:21-07:00

Post by t.hoepfner »

Here are links to two simple images which demonstrate my problem:

http://web.dd-com.de/download/IM/cliptest01.tif
http://web.dd-com.de/download/IM/cliptest01.jpg

The upper rectangle is surrounded by a clipping path, so I'd expect to see something when doing a "convert -clip -negate foo bar".

Here's the output I get for 6.3.0:

[mbp:~/Desktop] th% convert -clip -negate cliptest01.jpg jpg630.jpg
[mbp:~/Desktop] th% convert -clip -negate cliptest01.tif tif630.jpg
convert: no clip path defined `cliptest01.tif': Bad file descriptor.

Here's the output I get for 6.3.1:

[mbp:~/Desktop] th% convert -clip -negate cliptest01.jpg jpg631.jpg
convert: no clip path defined `cliptest01.jpg': No such file or directory.
[mbp:~/Desktop] th% convert -clip -negate cliptest01.tif tif631.jpg

Links for the generated files:

http://web.dd-com.de/download/IM/jpg630.jpg
http://web.dd-com.de/download/IM/jpg631.jpg
http://web.dd-com.de/download/IM/tif630.jpg
http://web.dd-com.de/download/IM/tif631.jpg


Timo
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

With 6.3.1-2 we get this clipping path:
  • <?xml version="1.0" encoding="iso-8859-1"?>
    <svg width="1000" height="2000">
    <g>
    <path style="fill:#00000000;stroke:#00000000;stroke-width:0;stroke-antialiasing:false" d="
    M 71.8563,77.961
    L 949.102,77.961
    L 949.102,1031.48
    L 59.8803,1031.48
    "/>
    </g>
    </svg>
and with this command:
  • convert cliptest01.tif -clip -negate tif631.jpg
We get a white rectangle on top surrounded by a black border and a black rectangle on bottom as we think is expected.
t.hoepfner
Posts: 18
Joined: 2006-12-18T14:41:21-07:00

Post by t.hoepfner »

Hi,

thanks for the quick reply.
magick wrote: With 6.3.1-2 we get this clipping path:
...


This is the same I get (for the TIFF and 6.3.1-2).
magick wrote: and with this command:
  • convert cliptest01.tif -clip -negate tif631.jpg
We get a white rectangle on top surrounded by a black border and a black rectangle on bottom as we think is expected.


Yes, this is what I would have expected, but here I get two white rectangles (I also tried with the different order of the options).
I just cross-checked again on Windows with the binary available from the home page. Same (wrong) result as with my self-compiled one.

Any hint, where I could start searching for the problem?

I will try to setup a Linux in a virtual machine and see if I can reproduce your (correct) behaviour with the test files.

Thanks again for your help,

Timo
t.hoepfner
Posts: 18
Joined: 2006-12-18T14:41:21-07:00

Post by t.hoepfner »

Update:

I installed Fedora Core as a VM in Parallels and installed the Linux RPM. There, the

convert in -clip -negate out

works indeed. So it works OK on linux, but not on MacOS X and Windows... Any ideas how to continue from here?

The Linux version also cannot find the clipping path in the JPG like the other 6.3.1-2 versions I tested. This was working with the 6.3.0 version (on OSX) I was using earlier today, but that one failed on TIFFs.

One more question, please: How can I tell convert, to remove/whiten everything outside of the clipping path?

Thanks,

Timo
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post by magick »

Use +clippath #1 to apply an operator to the image outside the clipping path.
t.hoepfner
Posts: 18
Joined: 2006-12-18T14:41:21-07:00

Post by t.hoepfner »

magick wrote: Use +clippath #1 to apply an operator to the image outside the clipping path.


Mh, can't get it going... What's wrong?
  • [th@localhost ~]$ uname -a
    Linux localhost.localdomain 2.6.18-1.2798.fc6 #1 SMP Mon Oct 16 14:37:32 EDT 2006 i686 i686 i386 GNU/Linux
    [th@localhost ~]$ convert -version
    Version: ImageMagick 6.3.1 12/15/06 Q16 http://www.imagemagick.org
    Copyright: Copyright (C) 1999-2007 ImageMagick Studio LLC

    [th@localhost imtest]$ convert cliptest01.tif -clip -negate out.jpg
    # OK

    [th@localhost imtest]$ convert cliptest01.tif -clip +clip-path '#1' -negate out.jpg
    convert: missing an image filename `out.jpg'.
    [th@localhost imtest]$ convert cliptest01.tif -clip +clip-path '#1' out.jpg
    convert: missing an image filename `out.jpg'.
    [th@localhost imtest]$ convert cliptest01.tif +clip-path '#1' out.jpg
    convert: missing an image filename `out.jpg'.
Timo
Tobbe

Post by Tobbe »

Did you find any solution to this?
I have IM 6.3.1 on Windows XP and can't get any clipping to work.
t.hoepfner
Posts: 18
Joined: 2006-12-18T14:41:21-07:00

Post by t.hoepfner »

Tobbe wrote: Did you find any solution to this?
I have IM 6.3.1 on Windows XP and can't get any clipping to work.


No, I didn't find a solution. I'm still interested in finding one, though.

Currently, my workaround is quite complicated: Transfer the file via SFTP to some other server, which is running some prepress OPI server, run some commands to do the clipping via SSH and transfer back the result via SFTP.

I would love to replace that with ImageMagick...


Timo
Tobbe

Post by Tobbe »

I have been looking into this before (about a year ago or something).
Then I had another version of IM and I remember that I finally managed to get it working then. But I can't find the files and don't remember what version of IM I had then :-)
Post Reply