frugofrog has asked for the wisdom of the Perl Monks concerning the following question:

Hi I can't find the answer with google so I'm posting here. I would like to know how to check if a jpeg is a black and white image.
Can one do it with GD or other library available from CPAN?
Right now I'm using PerlMagick. But I would prefer a pure perl solution as PM is rather slow and I am using only one function from the whole API it has to offer.

Thank you for any suggestions

Replies are listed 'Best First'.
Re: check if image is monochromatic
by JavaFan (Canon) on Oct 30, 2010 at 20:26 UTC
    A *jpeg* image? Considering jpeg compression isn't lossless, and it has serious problems with sharp colour changes (which are very common in black-and-white images), you'd be hard pressed to find a black-and-white image, that's still just black and white after a compression/decompression cycle.

    Having said that, one way of doing it (untested):

    my @out = `djpeg $file.jpg | ppmhist`; splice @out, 0, 2; if (@out == 2 && ($out[0] =~ /^\s*0+\s+0+\s+0\s/ && $out[1] =~ /^\s*255\s+255\s+255\s/ || $out[0] =~ /^\s*255\s+255\s+255\s/ && $out[1] =~ /^\s*0+\s+0+\s+0\s/)) { ... is black-and-white ... } else { ... has colours that are neither black, nor white ... }
Re: check if image is monochromatic
by kcott (Archbishop) on Oct 31, 2010 at 02:24 UTC

    GD has this method: $image->colorsTotal().

    -- Ken

Re: check if image is monochromatic
by BrowserUk (Patriarch) on Oct 31, 2010 at 12:36 UTC

    I have to concur with the others, in that you need to clarify what you mean by "a black and white image"?

    (Which, BTW, is not the same as "monochromatic".)

    If, for example, you truly mean an image that consists only of pure black & pure white pixels, then the process of discovering that is non trivial for arbitrary jpegs.

    For palletised images--those where the actual rgb values used within the image are stored in a table (palette), and the pixels are represented by indexes into that table--even if the number of entries in the palette is 2, those two colors could be any two colors, not just black and white.

    Equally, there might be dozens, or even hundred of colors in the palette table, but they might all be set to either black or white.

    And for a "truecolor" image--24-bit rgb or 32-bit rgba--the only way to know what colors are used, is to inspect every pixel. Depending upon the tool being used, that can take considerable time on large images.

    Using GD on a 1024x768 24-bit color image of a mandrill monkey, it takes just over a second to inspect every pixel.

    But what most people mean when they say black&white or monochrome, is greyscale. Where each pixel "color" actually describes its relative luminance on a linear scale from no reflection (black) to full reflection (white). Even then, the number of steps on the scale between the limits can be as few as four, or as many as 256. And they can be stored as either palletised or truecolor file formats.

    And that gets more complicated by the fact that the human eye will perceive rgb(0x81,0x80,0x80) (and similar variations) as the same as rgb(0x80,0x80,0x80). So, whilst the human eye might perceive an image as "black&white", a program looking strictly for colors with equal r,g,b components would reject it.

    And once we get into the realms of human perception, and the effects context can have upon our perception of color, things get much more difficult. To see some of these effects, browse the examples at: http://www.bbc.co.uk/news/magazine-11553099. (Or if you have an hour to spare, watch: Is seeing believing It's well worth your time!)

    So, you really need to decide quite what you mean by "black & white" or "monochrome" before any good solution to the question can be suggested.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Equally, there might be dozens, or even hundred of colors in the palette table, but they might all be set to either black or white.

      Ah, yes! This program outputs such an image: a bmp image with lots of identical colors (white) in its palette. It also has unused colors that are neither black or white. (By the way, the image is also an executable perl script.)

      print pack"V*",589712706,0,70647808,2621440,9437184,3604480,65536,8, 519045120,(309592064)x2,(16777216)x2,-65536,(-1)x95,65535,-65536, (-1)x30,571146239,1919957794,578055785,1953723722,1869504800, 1919248500,1919242272,1634213996,1919249251,577657900,1313169247, 174022468,-246,(-1)x115,65535,(0)x624,6316128,0,1616904192, 1610637408,24672,1616928864,96,1610612736,24672,1616928768,(0)x25, (1610612736)x2,0,24576,1616904192,0,24576,(0)x2,96,6291456,(0)x25, 6291456,0,96,24576,1610612736,0,24576,(0)x2,96,6291456,(0)x25, 6291456,0,96,24576,1610612736,0,24576,(0)x2,96,6291456,(0)x25, 6291456,0,96,1616904192,1616928864,0,1616928768,6316128,0,96, 6291456,(0)x27,96,0,1610612736,0,24576,1610612736,0,96,6291456, (0)x27,96,6291456,1610612736,0,24576,0,96,24672,6291456,(0)x27, 96,1610612736,6316128,0,24576,0,96,1616904288,24672,(0)x27,96,(0)x3, 24576,0,(96)x2,(0)x28,96,(0)x3,24576,1610612736,0,96,(0)x27, 1616928768,6316128,(0)x3,1616928864,6316128,0,96,(0)x34,1610612736, 96,(0)x951;

      Alternately you could use a real compressor module:

      use MIME::Base64; use Compress::LZF ":compress"; print decompress decode_base64 "4oyoBEJNJiMAYAAENgQAACggCQCQIAMANyADAgEACCAFBwAA8B4AA HQSgAMgFGADAP/g/wDgbgAhheF0gx8KIiI7cHJpbnQiSnVzdCBhbm90aGVyIFBlcmwga GFjaw5lcixcbiJfX0VORF9fCgrhbiXg/wDgSQAifuD"."/AOD"x8 ."/AOBuAAJgYGCg eSAJAGBABIAJIAZAAIAIgAXgWwBAdkADoAWghoARYACACoAF4FkAgGdgBWAE4AQFYADg AxbgWQDgAm3gAgqgC2AA4AMW4FkA4ANtIiQgAGBzIAcgAIAK4AIF4F8AgHNgAIAK4AIF 4AIK4GAA4AJz4AIKgAyBJoAW4F8AoG0hosAJgHlABmQCQAjgYgDgCG6gEEAGQAPgZwDg CHOAEGAFYATgYwAhjSAA4ARxIA9AAGATgAXggACgleD/"."AOD/"x13 .AOBfAAEAAA;

        Very cute! (Interestingly, ifranview lists the number of colors as 2.)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: check if image is monochromatic
by cmac (Monk) on Oct 31, 2010 at 03:19 UTC
    When many people say "black & white" they really mean "shades of gray". If that's what you mean, and your system has (or you can get) djpeg and ppmhist as suggested by JavaFan, you might then process the @out array to see if all of the color triplets have 3 values "close together" which indicates grayscale. First look at the results of piping these as he suggests "by hand" and see what the the numbers look like.
Re: check if image is monochromatic
by zentara (Cardinal) on Oct 31, 2010 at 11:47 UTC
    Here is a little Image::Magick script that may help you analyze image colors.
    #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $file = shift or die "Need a file $!\n"; my $img = Image::Magick->new; $img->ReadImage($file); # if you want a pixel by pixel list of every color # a huge slow output #$img->Set(magick => 'txt'); #$img->Write("$0.txt"); #histogram #returned values are an array of #red, green, blue, opacity, and count values. my $tot = 0; my (@colors) = $img->Histogram(); #print join "\n\n", @colors; while (1){ if (scalar @colors == 0){last} my $r = shift @colors; my $g = shift @colors; my $b = shift @colors; my $o = shift @colors; my $count = shift @colors; $tot++; print "$count ($r,$g,$b) at $o opacity\n"; } print "\n$tot total colors\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh