How to know the image's transparent space?

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?".
niyanwen
Posts: 71
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

How to know the image's transparent space?

Post by niyanwen »

If I have in image, which has transparent space, is there a way I can know the transparent space starts from x and y? basically like this image boundary?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to know the image's transparent space?

Post by fmw42 »

Please always identify your IM version and platform and post your input and output images to some free hosting service and put the URLs here so we can see what you are trying to do.

Just check the pixel color at your desired coordinate.

Code: Select all

convert image -format "%[pixel:u.p{x,y}]" info:
If the alpha value is less than 1, it has some transparency there.

Example:

Code: Select all

convert logo: -transparent white -format "%[pixel:u.p{10,10}]" info:
none
none means the same as perfectly transparent

Note that logo: is an internal ImageMagick images. Replace that with your image in the form, imagename.suffix

This does not tell you that it starts there, just that that pixel is transparent.



To find the first fully transparent pixel in the image in row/column format, you can do that in ImageMagick by extracting the alpha channel and using identify.

For example, I take the logo: image and make white transparent. Then I extract the alpha channel and negate it so that transparency is represented as white rather than black. Then just for fun, I add a 10 pixel black border, so that the first white pixels will be at 10,10. Then, I use identify.

Code: Select all

convert logo: -transparent white -alpha extract -negate -bordercolor black -border 10x10 x.png
identify -precision 5 -define identify:locate=maximum -define identify:limit=1 x.png
Channel maximum locations:
Red: 65535 (1) 10,10
Green: 65535 (1) 10,10
Blue: 65535 (1) 10,10

So all channels are brightest (maximum value) at 10,10
niyanwen
Posts: 71
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: How to know the image's transparent space?

Post by niyanwen »

Hi

thanks for your answer, I want to explain a little, so I have an image to print using some printer
I want to get this image boundary and exclude transparency. since printer needs to know image boundary

another thought I have, could I use trim function to trim the transparency of this image, and calculate the image boundaries with original image size?
like that,

IMOperation op = new IMOperation();
op.add(image);

op.bordercolor("none");
op.border(1,1);

op.trim();
op.p_repage();
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to know the image's transparent space?

Post by fmw42 »

If you want to trim all transparency on the outside, then see the new -define for trim.

Use -define trim:percent-background=0% to remove all the background from the image. Example

input:
Image

Code: Select all

magick img.png -fuzz 5% -background black -alpha background -define trim:percent-background=0% -trim +repage img_atrim.png
Image
niyanwen
Posts: 71
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: How to know the image's transparent space?

Post by niyanwen »

Hi,

after revisiting my use case, I think what I need is
this is original picture https://m.media-amazon.com/images/G/01/ ... IMAGE1.png
I want to know know x, y direction basically need to know left boundary, and top boundary which don't have transparency
in here top boundary is 583 and left boundary is 555

then I need to trim these transparency, now my question is how can I know top boundary and left boundary using image magick
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to know the image's transparent space?

Post by fmw42 »

use

Code: Select all

convert SHIRT_ART_IMAGE1.png -format "%@" info:
3390x3004+555+593

That will give you the trim coordinates WxH+Xoff+Yoff. The Xoff and Yoff are the top left corner where it would have been trimmed to get rid of transparency. So the coordinates you want, I think, are 555,593
niyanwen
Posts: 71
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: How to know the image's transparent space?

Post by niyanwen »

wow, great, thank you so much!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to know the image's transparent space?

Post by fmw42 »

You can trim and get the coordinates at the same time.

Code: Select all

convert SHIRT_ART_IMAGE1.png -format "%@" +write info: -trim +repage SHIRT_ART_IMAGE1_trim.png
3390x3004+555+593

But you also get the trimmed image.
niyanwen
Posts: 71
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: How to know the image's transparent space?

Post by niyanwen »

when I trim it, can I add some padding there, for example, have a padding 5 around the border when trim it?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to know the image's transparent space?

Post by fmw42 »

Code: Select all

convert SHIRT_ART_IMAGE1.png -format "%@" +write info: -trim +repage -bordercolor none -border 5 SHIRT_ART_IMAGE1_trim.png
niyanwen
Posts: 71
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: How to know the image's transparent space?

Post by niyanwen »

thanks, question for my own learning, for printing purpose, we only need to know top, left boundary, which is x, y and we don't need to know right, and bottom boundary right?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to know the image's transparent space?

Post by fmw42 »

Sorry, I do not know much about printing issues. But I think you need to know the width and height as well.
niyanwen
Posts: 71
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: How to know the image's transparent space?

Post by niyanwen »

Hi

something might be related, I also want to detect if this image is fully transparent, is there anything I can tell from this output, or I need to do it before running this command? for if it is transparent, I don't need to trim it
niyanwen
Posts: 71
Joined: 2019-05-23T16:19:12-07:00
Authentication code: 1152

Re: How to know the image's transparent space?

Post by niyanwen »

basically can I assume if output xOffset == src.getWidth() && yOffset == 0, sec is the original image before trim, I can say this image is total transparent?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to know the image's transparent space?

Post by fmw42 »

niyanwen wrote: 2019-06-26T14:06:17-07:00 basically can I assume if output xOffset == src.getWidth() && yOffset == 0, sec is the original image before trim, I can say this image is total transparent?
No, you cannot. It could be transparent somewhere in the middle.

So if you care about transparency anywhere in your image, then you need to test the alpha channel to see if its average (mean) is fully opaque value of 1 with a %[fx:mean] evaluation.

Code: Select all

convert image -channel a -separate -format "%[fx:mean]" info:
A value less than 1 will indicate that there is some transparency somewhere in the image.

You can do it also as

Code: Select all

convert image -channel a -separate -format "%[fx:mean<1?1:0]" info:
If it returns 1, it has some transparency. If it returns 0, it is fully opaque
Post Reply