Hi,
I would like to check if the edges of a batch of images are transparent or above a certain level of transparency..
so I'm thinking to first quickly check the four corner pixels of each image for transparency..(followed perhaps by the centers of the edges and then moving out..
How would I do this in a concise manner?
Thanks for all help
check transparency of specific pixel
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: check transparency of specific pixel
If scripting, crop the corners and output them using txt: corners can be selected using gravity.
If you know the size of the image you can use -chop with a size 2 pixels smaller to get all four corner pixels at the same time. For example a 100x100 pixel image...
If you don't know the size of the image. a roll can move all four corner pixels to the top left corner where you can crop them out.
See IM examples, TXT: Enumerated Pixel Image File Format
http://www.imagemagick.org/Usage/files/#txt
Rather than outputing a txt image file format of the pixel information you can directly output the transparency (alpha) value as a floating point number from 0.0 to 1.0 by using a FX Escape
or as a 8 bit value by replacing the escape with '%[fx:int(a*255)]'
Of course the ideal method would be to just do a pixel lookup from an API. that way you can read the image into memory, check it than continue doing operations on the image without destorying or having to re-read or re-initialize IM.
Code: Select all
convert image.png -gravity SouthEast -crop 1x1+0+0 txt:
Code: Select all
convert image_100x100.png -chop 98x98+1+1 txt:
Code: Select all
convert image.png -roll +1+1 -chop 2x2+0+0 txt:
http://www.imagemagick.org/Usage/files/#txt
Rather than outputing a txt image file format of the pixel information you can directly output the transparency (alpha) value as a floating point number from 0.0 to 1.0 by using a FX Escape
Code: Select all
convert image.png -gravity SouthEast -crop 1x1+0+0 -format '%[fx:a]' info:
Of course the ideal method would be to just do a pixel lookup from an API. that way you can read the image into memory, check it than continue doing operations on the image without destorying or having to re-read or re-initialize IM.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: check transparency of specific pixel
Thanks Anthony for all the info.
I am using PHP shell commands to run IM (inside MAMP on Mac OS X)
As I have to check arrays of many many images, I am interested in your suggestion to lookup from an API for speed ..
Excuse my ignorance, but would this mean using PHP to check the pixel instead?
ta
I am using PHP shell commands to run IM (inside MAMP on Mac OS X)
As I have to check arrays of many many images, I am interested in your suggestion to lookup from an API for speed ..
Excuse my ignorance, but would this mean using PHP to check the pixel instead?
ta
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: check transparency of specific pixel
The PHP api's is command line system calls, or the 'imagick' PHP API.tobycarr wrote:Thanks Anthony for all the info.
I am using PHP shell commands to run IM (inside MAMP on Mac OS X)
As I have to check arrays of many many images, I am interested in your suggestion to lookup from an API for speed ..
Excuse my ignorance, but would this mean using PHP to check the pixel instead?
ta
The latter would probably be better for database work, though there is not as much help on it.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: check transparency of specific pixel
OK thanks Anthony..for now I have started using a bit of PHP code:
for instance to check top left pixel....
..and looking at value of
Do you think this would be fast enough compared to using command line system calls as you suggest (not knowing how to install the Imagick PHP API yet so leaving that for another day)..?
for instance to check top left pixel....
Code: Select all
$image=imageCreateFromJPEG($filename);
$x = 0;
$y = 0;
$colorindex = imagecolorat($image,$x,$y);
$colorrgb = imagecolorsforindex($image,$colorindex);
Code: Select all
$colorrgb[alpha]
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: check transparency of specific pixel
It should be faster, as it has far less IO and file parsing. But more importantly you still have the image in memory to work with.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: check transparency of specific pixel
great ta Anthony - I am using now and seems to be ripping through ok