How can I check a blank image in Qt or PyQt?

I created a collection of images. Some of them are empty, as they are white in the background. 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 a better way to do this in Python?

+4
source share
2 answers

I don't know about Qt, but there is a simple and efficient way to do this 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 applies only to single-band images.

+5
source

Well, I would count the colors in the image. If there is only one, the image will be blank. I don’t know enough Python or qt to write code for this, but I’m sure there is a library that can tell you how many colors are in the image (I’m going to learn how to use ImageMagick for this right after publication).

Update: Here is the Perl (apology) code to do this using Image :: Magic . You should be able to convert it to Python using 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__ 
+1
source

All Articles