Page 1 of 1

check transparency of specific pixel

Posted: 2009-05-11T16:45:09-07:00
by tobycarr
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

Re: check transparency of specific pixel

Posted: 2009-05-11T17:13:11-07:00
by anthony
If scripting, crop the corners and output them using txt: corners can be selected using gravity.

Code: Select all

   convert image.png  -gravity SouthEast -crop 1x1+0+0 txt:
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...

Code: Select all

   convert image_100x100.png -chop 98x98+1+1 txt:
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.

Code: Select all

   convert image.png -roll +1+1 -chop 2x2+0+0 txt:
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

Code: Select all

   convert image.png  -gravity SouthEast -crop 1x1+0+0  -format '%[fx:a]' info:
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.

Re: check transparency of specific pixel

Posted: 2009-05-11T18:30:57-07:00
by tobycarr
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

Re: check transparency of specific pixel

Posted: 2009-05-11T18:47:35-07:00
by anthony
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 PHP api's is command line system calls, or the 'imagick' PHP API.
The latter would probably be better for database work, though there is not as much help on it.

Re: check transparency of specific pixel

Posted: 2009-05-11T19:56:10-07:00
by tobycarr
OK thanks Anthony..for now I have started using a bit of PHP code:
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);
..and looking at value of

Code: Select all

$colorrgb[alpha]
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)..?

Re: check transparency of specific pixel

Posted: 2009-05-11T22:24:38-07:00
by anthony
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.

Re: check transparency of specific pixel

Posted: 2009-05-12T05:17:05-07:00
by tobycarr
great ta Anthony - I am using now and seems to be ripping through ok