tags:

views:

127

answers:

2

I have generated a collection of images. Some of them are blank as in their background is white. I have access to the QImage object of each of the images. Is there a Qt way to check for blank images? If not, can someone recommend the best way to do it in Python?

A: 

Well, I would count the colors in the image. If there is only one, then the image is blank. I do not know enough Python or qt to write code for this but I am sure there is a library that can tell you how many colors there are in an image (I am going to look into using ImageMagick for this right after I post this).

Update: Here is the Perl code (apologies) to do this using Image::Magick. You should be able to convert it to Python using the Python bindings.

Clearly, this only works for palette based images.

#!/usr/bin/perl

use strict;
use warnings;

use Image::Magick;

die "Call with image file name\n" unless @ARGV == 1;
my ($file) = @ARGV;

my $image = Image::Magick->new;

my $result = $image->Read( $file );
die "$result" if "$result";

my $colors = $image->Get('colors');

my %unique_colors;

for ( my $i = 0; $i < $colors; ++$i ) {
    $unique_colors{ $image->Get("colormap[$i]") } = undef;
}

print "'$file' is blank\n" if keys %unique_colors == 1;

__END__
Sinan Ünür
+1  A: 

I don't know about Qt, but there is an easy and efficient way to do it in PIL Using the getextrema method, example:

im = Image.open('image.png')
bands = im.split()
isBlank = all(band.getextrema() == (255, 255) for band in bands)

From the documentation:

im.getextrema() => 2-tuple

Returns a 2-tuple containing the minimum and maximum values of the image. In the current version of PIL, this is only applicable to single-band images.

Nadia Alramli
There's a typo in your code, it's actually getextrema instead of getexterma and it checks for blanks well.
Thierry Lam
@Thierry, thanks for pointing this out. Fixed
Nadia Alramli