Trimming using a mask
-
- Posts: 13
- Joined: 2014-09-26T08:23:47-07:00
- Authentication code: 6789
Trimming using a mask
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:
Variable Mask:
Desirable result:
Does anyone know how to achieve this result?
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:
Variable Mask:
Desirable result:
Does anyone know how to achieve this result?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Trimming with a mask
Windows BAT syntx:
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).
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
snibgo's IM pages: im.snibgo.com
-
- Posts: 13
- Joined: 2014-09-26T08:23:47-07:00
- Authentication code: 6789
Re: Trimming using a mask
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Trimming using a mask
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:
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Trimming using a mask
Does it do the required crop?fmw42 wrote:This works for me on IM 6.9.0.0 Q16 Mac OSX.
snibgo's IM pages: im.snibgo.com
-
- Posts: 13
- Joined: 2014-09-26T08:23:47-07:00
- Authentication code: 6789
Re: Trimming using a mask
I just tried it and it doesn't.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Trimming using a mask
Sorry I did not realize you wanted it cropped. Try this
or perhaps more accurate
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.
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
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:
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
I guess the lines under are indicating they are links and are input by the form code.
-
- Posts: 13
- Joined: 2014-09-26T08:23:47-07:00
- Authentication code: 6789
Re: Trimming using a mask
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!