Trimming using a mask

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
marcospassos
Posts: 13
Joined: 2014-09-26T08:23:47-07:00
Authentication code: 6789

Trimming using a mask

Post by marcospassos »

Hello,

I'm trying to trim an image using a given mask. The problem is that my image has transparent areas and I need to cut a given area, preserving the transparent areas, so I can't just apply the mask and then use the trim function. Once mask is variable, I don't know the positioning or the dimension of the white area.

Given the input image:
Image

Variable Mask:
Image

Desirable result:
Image

Does anyone know how to achieve this result?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trimming with a mask

Post by snibgo »

Windows BAT syntx:

Code: Select all

convert C7PBvV2.png -format "%%@" info:
rem Result is 179x179+388+76

convert ^
  YfAsWs5.png ^
  ( +clone ^
    -alpha extract ^
    C7PBvV2.png ^
    -compose Darken -composite ^
  ) ^
  -compose CopyOpacity -composite ^
  -crop 179x179+388+76 +repage ^
  out2.png
To get the text output from the first command into the second needs some scripting, which depends on what language you use (bash, Windows cmd, etc).
snibgo's IM pages: im.snibgo.com
marcospassos
Posts: 13
Joined: 2014-09-26T08:23:47-07:00
Authentication code: 6789

Re: Trimming using a mask

Post by marcospassos »

Is there any other way to achieve it without using the trim bounding box? This functions calls the function 'GetImageBoundingBox', which is part of the ImageMagick core API (unstable API), unavailable in most of ImageMagicks libraries, such as Imagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trimming using a mask

Post by fmw42 »

This works for me on IM 6.9.0.0 Q16 Mac OSX. Simply extract the alpha channel from the image and multiply by the mask. Then put the new alpha channel back into the image.

Code: Select all

convert YfAsWs5.png \
\( -clone 0 -alpha extract C7PBvV2.png -compose multiply -composite \) \
-alpha off -compose copy_opacity -composite show:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trimming using a mask

Post by snibgo »

fmw42 wrote:This works for me on IM 6.9.0.0 Q16 Mac OSX.
Does it do the required crop?
snibgo's IM pages: im.snibgo.com
marcospassos
Posts: 13
Joined: 2014-09-26T08:23:47-07:00
Authentication code: 6789

Re: Trimming using a mask

Post by marcospassos »

I just tried it and it doesn't.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trimming using a mask

Post by fmw42 »

Sorry I did not realize you wanted it cropped. Try this

Code: Select all

convert YfAsWs5.png \
\( -clone 0 -alpha extract C7PBvV2.png -compose multiply -composite \) \
-alpha off -compose copy_opacity -composite -trim +repage \
\( -clone 0 -alpha transparent \) -reverse +append result.png
or perhaps more accurate

Code: Select all

geometry=`convert C7PBvV2.png -format "%@" info:`
convert \( YfAsWs5.png -crop $geometry +repage \) \
\( -clone 0 -alpha extract \) \
\( C7PBvV2.png -crop $geometry +repage \) \
\( -clone 1,2 -compose multiply -composite \) \
-delete 1,2 -alpha off -compose copy_opacity -composite show:
See string format %@ at http://www.imagemagick.org/script/escape.php for getting crop geometry.

Both methods work, but remove the line at the bottom. Why are there lines under the images on the your examples above? When I download them, they have no lines. So perhaps this is what you want anyway. If the lines are real, then they should be included in your image or added after the processing.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Trimming using a mask

Post by Bonzo »

I guess the lines under are indicating they are links and are input by the form code.
marcospassos
Posts: 13
Joined: 2014-09-26T08:23:47-07:00
Authentication code: 6789

Re: Trimming using a mask

Post by marcospassos »

The lines are from the links. I managed to reproduce the trim bounding box function using the API and it has solved my problem. Thanks a lot, guys!
Post Reply