Is possible to check if a PNG image has transparent background?
Asked Answered
M

3

8

I'm using a crop & resize function for images, but I need to let it crop/resize ONLY png files WITH transparent backgrounds, at least 1 pixel in the image should be transparent for the image to be accepted.

Is possible to check if a PNG image has transparent background/pixels?

I'm using PHP and GD libraries.

EDIT: Ok, I've figured out how to do this on PHP with GD libraries. Look how clean it looks! :)

<?php

$im = imagecreatefrompng("php.png");
$rgba = imagecolorat($im,1,1);
$alpha = ($rgba & 0x7F000000) >> 24;

var_dump($alpha);
?>

Any ideas how to do an array for the x/y coordenates to check all the image pixels looking for at least 1 pixel = $alpha = 127?

Moonshot answered 11/1, 2012 at 13:6 Comment(4)
It's possible, e.g. by exhaustive checking, but is there a specific PNG framework or language you'd like answers to refer to?Demonetize
Would it take too much processing time to check pixel by pixel? I'll try it.Moonshot
That depends on the image size...Generally if you process many files of large size then yes - it will be too much processing time...Rarebit
I've figured it out! just need to learn how to create an array or loop for every pixel on the image. Can you help?Moonshot
O
3

You can call out to imagemagick for it:

https://www.imagemagick.org/discourse-server/viewtopic.php?t=18596

convert my_image.png -format "%[opaque]" info:

False

Oscillation answered 19/10, 2019 at 22:46 Comment(0)
F
1

Well you can certainly run through all the pixels and check to see if any of them have an alpha that is not 255. What language and libraries are you using?

Ferro answered 11/1, 2012 at 13:9 Comment(4)
I'm using GD and PHP/jQuery. Any ideas how to do that?Moonshot
I'm afraid not. I've done what you are trying to do in Java and python but do not know much php. Still, I would search for an image library and see what they offer. Either they will have a function that will tell you, or you can figure it out with the following pseudo code: is a pixel 32 bits? yes==possibly transparent. (search out whether 8 bit might be, 24 is not). if possibly transparent, get the data for the image, loop through it, check each pixel to see if it is transparent.Ferro
I know, I'm not a php savy neither, just learning. I can't found anything, I think GD libraries doesn't have anything for my purposes, I've found just a few functions for alpha or transparent, but nothing that works for me. Maybe I should try to do the entire thing in other language? :( I will try with imagecolorat, maybe I can do something with that function.Moonshot
I've figured it out! just need to learn how to an array or loop for every pixel on the image.Moonshot
W
1

One way to handle this in Imagemagick is to check the mean value of the alpha channel. If 1, then it is opaque. Otherwise, the alpha channel has some transparency either somewhere or partial transparency.

convert logo: -channel a -separate -format "%[fx:mean]" info:
1

convert logo: -transparent white -format "%[fx:mean]" info:
0.894907


So you can do:

convert image -format "%[fx:mean==1?1:0]" info:


If the return value is 1, then it is fully opaque. If the return value is 0, then there is some transparency somewhere.

Width answered 20/10, 2019 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.