How to know the image's transparent space?
How to know the image's transparent space?
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?
- 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?
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.
If the alpha value is less than 1, it has some transparency there.
Example:
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.
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
Just check the pixel color at your desired coordinate.
Code: Select all
convert image -format "%[pixel:u.p{x,y}]" info:
Example:
Code: Select all
convert logo: -transparent white -format "%[pixel:u.p{10,10}]" info:
none
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
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
Re: How to know the image's transparent space?
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();
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();
- 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?
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:
Use -define trim:percent-background=0% to remove all the background from the image. Example
input:
Code: Select all
magick img.png -fuzz 5% -background black -alpha background -define trim:percent-background=0% -trim +repage img_atrim.png
Re: How to know the image's transparent space?
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
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
- 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?
use
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
Code: Select all
convert SHIRT_ART_IMAGE1.png -format "%@" info:
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
Re: How to know the image's transparent space?
wow, great, thank you so much!
- 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?
You can trim and get the coordinates at the same time.
3390x3004+555+593
But you also get the trimmed image.
Code: Select all
convert SHIRT_ART_IMAGE1.png -format "%@" +write info: -trim +repage SHIRT_ART_IMAGE1_trim.png
But you also get the trimmed image.
Re: How to know the image's transparent space?
when I trim it, can I add some padding there, for example, have a padding 5 around the border when trim it?
- 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?
Code: Select all
convert SHIRT_ART_IMAGE1.png -format "%@" +write info: -trim +repage -bordercolor none -border 5 SHIRT_ART_IMAGE1_trim.png
Re: How to know the image's transparent space?
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?
- 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?
Sorry, I do not know much about printing issues. But I think you need to know the width and height as well.
Re: How to know the image's transparent space?
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
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
Re: How to know the image's transparent space?
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?
- 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?
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:
You can do it also as
Code: Select all
convert image -channel a -separate -format "%[fx:mean<1?1:0]" info: