cut and keep remainder

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
strudel67

cut and keep remainder

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: cut and keep remainder

Post 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/
strudel67

Re: cut and keep remainder

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: cut and keep remainder

Post by fmw42 »

you are welcome. glad it helped
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: cut and keep remainder

Post 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/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply