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

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

Post by niyanwen »

I want to see if this image is fully transparent, if not I will trim this image as I mentioned above, so I cannot tell if it is transparent from detect transparency command right?

is there a way I can just know if this is a fully transparent image or not? I don't want to know some transparency

what is easiest and fastest way to do it? for we have a lot of image to processing, so want to reduce some time
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 »

in the other answer I saw you are using this command, is this the only way to do it?
convert transparent_image.png -channel a -separate -scale 1x1! -format "%[fx:mean]\n" info:
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 »

I do not fully understand your question. Please clarify. Do you care if opaque around the edges of the image, but transparent inside the image?

If you want to know if the image is fully transparent everywhere, then the result would be 0 from my command. You can also tell if the image is fully opaque (no transparency) by using

Code: Select all

convert image -format "%[opaque]" info:
If returns true, it is fully opaque. If returns false, it has some (partial) transparency somewhere in the image.

See https://imagemagick.org/script/escape.php

convert transparent_image.png -channel a -separate -scale 1x1! -format "%[fx:mean]\n" info:
That works also. It just averages the image to 1 pixel first. I am not sure which is faster. You would have to test both ways.
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 for you answer, in this case, I only care the image is fully transparent or not fully transparent, I think I would go with this command, if there is other shorter one, since I use java code, so shorter one might be clearer, but if not, it is fine
convert transparent_image.png -channel a -separate -scale 1x1! -format "%[fx:mean]\n" info:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

You don't need "-channel a -separate -scale 1x1" . Just use "%[fx:mean.a]", the mean of the alpha channel.
snibgo's IM pages: im.snibgo.com
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, it works

I have another question, so currently I am running two commands in my java application. one is scale image, the other is remove transparent space and get image offset, what I mentioned in this post, but it running is too slow, is there a way to increase the processing speed?
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 »

one interesting thing is it is much slower because I used 7.X version in my java application, but if I switched to 6.9 version, it is much faster, but one compare command is starts fail
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 »

one compare image are same command starts fail and detect transparent image command did not work also
image magick version: 6.9.4_1
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 »

This is an old post and the topics have changed since it was first posted. Please start a new question with your issue. It is not explained here very well.

ImageMagick 7 is slower than ImageMagick 6, because the default for ImageMagick 7 is HDRI enabled, which is not the case for ImageMagick 6. Also IM 6.9.4.1 is ancient.
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 »

got it, start a new post, thank you!
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,

when I used this command to trim the white space, if get output like 0x0+4500+5400, can I assume it is a fully transparent image when I use IM6? for the other method to take care of this is a bit slow, I want to combine to see if I can tell from this command
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 »

basically if output coordinates is 0x0 I mean, can I assume it is a fully transparent image?
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 »

Probably. But to test if an image is fully transparent, the best command would be

convert image.png -channel a -format "%[fx:mean]" info:

If 0, then fully transparent.
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 »

Yeah, but it takes too much time looks like, after getting rid of this command, time is durable now
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

niyanwen wrote:basically if output coordinates is 0x0 I mean, can I assume it is a fully transparent image?
"-format %@" gives 0x0 when all the pixels are the same. They might all be transparent, or all be opaque Red, or opaque White, or anything else.
snibgo's IM pages: im.snibgo.com
Post Reply