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.
RIP an inspiration; A true Folk's Guy

In reply to Re: check if image is monochromatic by BrowserUk
in thread check if image is monochromatic by frugofrog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.