Page 1 of 1

cut and keep remainder

Posted: 2009-10-23T05:25:36-07:00
by strudel67
I have a question about how to cut an image and keep the remainder and not what was cut. So for instance I have an image that is 1024x200. I want cut out a section that is 700x150 and discard that piece and keep the piece with a cut out. I haven't been able to find this anywhere? Thanks in advance.

Re: cut and keep remainder

Posted: 2009-10-23T09:41:37-07:00
by fmw42
You basically have to fill the region in the middle with transparency. Here are two ways.

This one works in general. Create a white image the size of the input (using +clone -threshold -1), insert a black area where you want the hole, then copy that to the alpha channel.

convert logo: \( +clone -threshold -1 -size 100x100 xc:black -gravity center -composite \) -compose copy_opacity -composite logo_hole1.png


This one works if you have no black in your image. You can choose some other color that is not in your image.
So you composite a black image the size of the hole onto your image and then make that color (black) into transparency.

convert logo: -size 100x100 xc:black -gravity center -composite -transparent black logo_hole2.png

I am sure there are other ways to do this. But these two come to mind.

The logo: image is an IM internal test image. You can substitute your image for it. See http://www.imagemagick.org/script/forma ... tin-images

You need to use either GIF or PNG for your output as they two formats support transparency. JPG does not.

See masking and transparency at http://www.imagemagick.org/Usage/channels/

Re: cut and keep remainder

Posted: 2009-10-23T10:50:04-07:00
by strudel67
fmw42 - that is EXACTLY what I needed - thank you thank you thank you. Spent 3 hours scouring last night (went through a bottle of wine which probably didn't help) but you have given me exactly what I needed. PERFECTO.

Hope I can someday return the fav.

Hans

Re: cut and keep remainder

Posted: 2009-10-23T13:25:58-07:00
by fmw42
you are welcome. glad it helped

Re: cut and keep remainder

Posted: 2009-10-24T19:18:39-07:00
by anthony
Their are two ways of specifying the part of the image you do not want, (or do want) One method is to use a gray scale (mostly black and white) mask, and using 'CopyOpacity'. The other is an alpha channel shape, and use 'DstIn' and 'DstOut' as appropriate.

See IM Examples, Composing, as well as Channel, Masks, and Transparency
http://www.imagemagick.org/Usage/compose/
http://www.imagemagick.org/Usage/channels/