2

I want to detect the black/almost black JPEG images from a folder using PERL. Do you have any suggestions about the method/module that I should use?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Nick
  • 331
  • 3
  • 14

1 Answers1

7

Dark images will generally have a low-ish mean pixel value.

You can get the mean of the image's pixels using ImageMagick's identify at the command line like this:

identify -format "%[mean]" input.png

or using

identify -verbose input.png

and looking for the parameter you think will help most.

Or use Perl like this:

#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;

my $image = Image::Magick->new;
$image->ReadImage("c.png");

print $image->Get("%[mean]");

In the Perl case, the range is 0-65535, so dark ones will have a mean below say 5,000.

Example:

Here is a dark image:

enter image description here

identify -format "%[mean]" dark.jpg
16914.6

And here is a lighter one:

enter image description here

identify -format "%[mean]" light.jpg
37265.7
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thank you, Mark. This would work perfectly to suit my needs, but I can't manage to install Image::Magick on StrawberryPerl Win7 x64. Magick.xs:60:31: fatal error: magick/MagickCore.h: No such file or directory compilation terminated. dmake.exe: Error code 129, while making 'Magick.o' JCRISTY/PerlMagick-6.89-1.tar.gz Z:\strawberryperl_86\c\bin\dmake.exe -- NOT OK I was checking this: http://www.imagemagick.org/discourse-server/viewtopic.php?f=7&t=25746&p=113771#p113771 but still could not fix it. – Nick Nov 01 '14 at 09:11
  • Not sure I can help there. I use ImageMagick at the commandline, with Perl and PHP on OS X. When I have used it on Windows I have used the commandline and always ActiveState Perl. Maybe try asking another question about getting IM installed on your Strawberry Perl - maybe not on SO as this about programming. – Mark Setchell Nov 01 '14 at 09:21
  • I'm searching a way to fix it right now. :) I'll reply back. – Nick Nov 01 '14 at 09:22
  • 1
    The range of pixel values depends on the "quantum depth" that is currently used (8, 16, or 32 bits). To write portable code, pixel values should be divided by the value returned by `Image::Magick->QuantumRange`. – nwellnhof Nov 02 '14 at 15:06
  • @nwellnhof Yes, agreed, that would be more portable. Thank you. – Mark Setchell Nov 02 '14 at 16:21