Page 1 of 1

Trimming using a mask

Posted: 2014-12-02T07:13:31-07:00
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?

Re: Trimming with a mask

Posted: 2014-12-02T07:36:14-07:00
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).

Re: Trimming using a mask

Posted: 2014-12-02T09:37:14-07:00
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.

Re: Trimming using a mask

Posted: 2014-12-02T09:47:28-07:00
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:

Re: Trimming using a mask

Posted: 2014-12-02T10:09:39-07:00
by snibgo
fmw42 wrote:This works for me on IM 6.9.0.0 Q16 Mac OSX.
Does it do the required crop?

Re: Trimming using a mask

Posted: 2014-12-02T10:22:20-07:00
by marcospassos
I just tried it and it doesn't.

Re: Trimming using a mask

Posted: 2014-12-02T10:26:14-07:00
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.

Re: Trimming using a mask

Posted: 2014-12-02T10:41:33-07:00
by Bonzo
I guess the lines under are indicating they are links and are input by the form code.

Re: Trimming using a mask

Posted: 2014-12-04T10:50:53-07:00
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!