Page 1 of 1

Detect non-white pixels in image

Posted: 2016-10-06T15:34:45-07:00
by jchan
Given an image, I want to detect whether it has any non-white pixels (basically anything that's not #FFFFFF). Any transparent pixels should also be ignored.

Is there a way to do this, maybe somehow parsing the data given from the code below?

Code: Select all

convert sample.png -format %c histogram:info:-
Thanks!

Re: Detect non-white pixels in image

Posted: 2016-10-06T16:55:26-07:00
by fmw42
What do you mean by "detect"? Do you just want to know if there are any non-white pixels or any transparent pixels? Or do you need the locations of every one?

Please always provide your IM version and platform, since syntax may differ.

An image will have non-white pixels if the mean value is not 1 in the following.

Unix syntax:

Code: Select all

test1=`convert image -alpha off -scale 1x1 -format "%[fx:u]" info:`
if [ "$test1" = "1" ]; then
echo "white"
else
echo "non-white"
fi
An image has transparent pixels if the result of the following is not 1

Code: Select all

test2=`convert image -alpha extract -scale 1x1 -format "%[fx:u]" info:`
if [ "$test1" = "1" ]; then
echo "fully opaque"
else
echo "has transparency"
fi

Re: Detect non-white pixels in image

Posted: 2016-10-06T16:56:14-07:00
by snibgo
Assuming your are not using HDRI, simply find the minimum pixel value: %[fx:minima]. If this isn't #ffffff or whatever your Q is, all the pixels are white.