I need to check if the PNG image that has constant size (512x512 pixels) has only white pixels and nothing else or not.
Is there an easy way to do this, preferably without checking every pixel manually? Maybe using ImageMagick?
For the first case, you can simply check if all the pixels are white by using something like if np. sum(img == 255) == (number of element in image, i.e., width*heigth) .
In this article, we'll try to check if the opened image is empty or not by using OpenCV (Open Source Computer Vision). To achieve this objective we will use cv2. imread() method, If this method read the image then it returns the image coordinates matrix otherwise it will return None.
How do you know if an image is black in Python? You can use opencv for color detection and numpy too. Then. The printed answer will be in bgr if it is (255,255,255) its white, if its (0,0,0) its black.
Color Bars With Average The avg gives us a handy way to tell if a color is high relative to the others: A pixel is reddish if the red value is over the avg. So here the first pixel is reddish and the other two are not.
I don't think there is a magic way of determining whether an image is white.
You'll probably just have to check all pixels, but you can have fast access to your image if you convert it to bitmap and, instead of using GetPixel()
, you lock the bitmap in memory using the LockBits()
method. Then you can work with the BitmapData
type and write your own, fast, GetPixel()
, as explained here: Working with BitmapData.
Edit:
Actually, I though of another way: you can create a plain white image of the same size, and then compare your image to that one by computing and comparing their hashes. Take a look at this: Comparing two images.
You can avoid parsing and loops and two-step tests by asking Imagemagick to tell you the answer.
If the mean of the pixels is 1.0 (which it has to be if all pixels are white) and also the width is 512 and the height is 512, the test below will output 1, else 0.
# Test a white 512x512 image => Result: 1
identify -format "%[fx:(mean==1)&&(w==512)&&(h==512)?1:0]" white.png
1
# Test a white 600x512 image => Result: 0
identify -format "%[fx:(mean==1)&&(w==512)&&(h==512)?1:0]" white600x512.png
0
# Test a gray image => Result: 0
identify -format "%[fx:(mean==1)&&(w==512)&&(h==512)?1:0]" gray90.png
0
Another simple solution in ImageMagick command line is to just average the image down to 1 pixel or compute the mean of the image and test if the value is 1 (in the range 0 to 1), where 1 is white and 0 is black.
Create a test image
convert -size 512x512 xc:white white.png
convert -size 512x512 xc:black black.png
convert -size 512x512 xc:gray gray.png
Method 1 -- scale to 1 pixel:
convert white.png -scale 1x1! -format "%t = %[fx:u]\n" info:
white = 1
convert black.png -scale 1x1! -format "%t = %[fx:u]\n" info:
black = 0
convert gray.png -scale 1x1! -format "%t = %[fx:u]\n" info:
gray = 0.494118
Method 2:
convert white.png -format "%t = %[fx:mean]\n" info:
white = 1
convert black.png -format "%t = %[fx:mean]\n" info:
black = 0
convert gray.png -format "%t = %[fx:mean]\n" info:
gray = 0.494118
You can also do (a ternary) test in the command line. 1 will be true and 0 will be false.
test=$(convert white.png -format "%[fx:mean==1?1:0]\n" info:)
echo $test
1
Testing using Unix conditional:
if [ $test -eq 1 ]; then
echo "full white"
else
echo "not white"
fi
full white
Note that fx: is general purpose calculator and u is just the pixel value. t gives the name of the images without suffix.
ImageMagick supports C# in the Magick.Net module. See https://github.com/dlemstra/Magick.NET
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With