check transparency of specific pixel

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?".
Post Reply
tobycarr

check transparency of specific pixel

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: check transparency of specific pixel

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tobycarr

Re: check transparency of specific pixel

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: check transparency of specific pixel

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tobycarr

Re: check transparency of specific pixel

Post 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)..?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: check transparency of specific pixel

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tobycarr

Re: check transparency of specific pixel

Post by tobycarr »

great ta Anthony - I am using now and seems to be ripping through ok
Post Reply