5

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?

Oleg Filimonov
  • 1,323
  • 4
  • 13
  • 36

6 Answers6

4

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
3

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.

Community
  • 1
  • 1
Eutherpy
  • 4,471
  • 7
  • 40
  • 64
2

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

fmw42
  • 46,825
  • 10
  • 62
  • 80
1

From the commandline, you can run

identify -verbose image

And look for

Channel statistics:
Pixels: 10
Gray:
  min: 65535 (1)
  max: 65535 (1)
  mean: 65535 (1)

If the image has a "min" that is not 65535, then it's not an entirely white image.

Glenn Randers-Pehrson
  • 11,940
  • 3
  • 37
  • 61
1

Use this code:

private bool IsBlankImage(string path)
{
    bool isBlank = true;
    Image img = Image.FromFile(path);
    Bitmap bmp = new Bitmap(img);
    for (int x = 0; x < bmp.Width && isBlank; x++)
        for (int y = 0; y < bmp.Height && isBlank; y++)
            if (bmp.GetPixel(x, y).Name != "ffffffff") isBlank = false;
    bmp.Dispose();
    img.Dispose();
    return isBlank;
}

Then Call the Function:

bool isBlank = isBlankImage("D:\\myImage.jpg");
if (isBlank) MessageBox.Show("The Image is Blank");
else MessageBox.Show("The Image is Not Blank");
Jpsy
  • 20,077
  • 7
  • 118
  • 115
0

You could use the method as described in the answer from Eutherpy. If you want to do more image operation and you need an advanced image library you might want to use the ImageMagick C# wrapper that I created called Magick.NET.

Below is a small sample of how you could check it in that library:

private static bool IsWhite(MagickImage image)
{
  var white = MagickColors.White;

  using (var pixels = image.GetPixels())
  {
    foreach (var pixel in pixels)
    {
      var color = pixel.ToColor();
      if (color != white)
        return false;
    }
  }

  return true;
}

static void Main(string[] args)
{
  using (var image = new MagickImage(@"c:\folder\yourimage.png"))
  {
    if (IsWhite(image))
      Console.WriteLine("The image is all white");
  }
}
dlemstra
  • 7,813
  • 2
  • 27
  • 43