determine if image is grayscale

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tjh

    determine if image is grayscale


    hi -

    i'm a bit new to PHP - i've been messing around with the GD2 image functions
    and was wondering:

    i see a lot of information about converting a color image to grayscale but i
    can't figure out how to determine whether an image (GIF or JPG) is grayscale
    in the first place.

    is there a way to do this in PHP?

    thanks for any advice!

    tjh


  • Andy Hassall

    #2
    Re: determine if image is grayscale

    On Thu, 09 Sep 2004 17:24:43 GMT, "tjh" <t.hickey@ten.t sacmoc> wrote:
    [color=blue]
    >i'm a bit new to PHP - i've been messing around with the GD2 image functions
    >and was wondering:
    >
    >i see a lot of information about converting a color image to grayscale but i
    >can't figure out how to determine whether an image (GIF or JPG) is grayscale
    >in the first place.
    >
    >is there a way to do this in PHP?[/color]

    If it's a paletted image, imagecolorsfori ndex(), or for a true colour image
    loop through the pixels and usage imagecolorat().

    All the colours should have their red, green and blue components equal, or
    near equal, if it's a greyscale image.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Pjotr Wedersteers

      #3
      Re: determine if image is grayscale

      Andy Hassall wrote:[color=blue]
      > On Thu, 09 Sep 2004 17:24:43 GMT, "tjh" <t.hickey@ten.t sacmoc> wrote:
      >[color=green]
      >> i'm a bit new to PHP - i've been messing around with the GD2 image
      >> functions and was wondering:
      >>
      >> i see a lot of information about converting a color image to
      >> grayscale but i can't figure out how to determine whether an image
      >> (GIF or JPG) is grayscale in the first place.
      >>
      >> is there a way to do this in PHP?[/color]
      >
      > If it's a paletted image, imagecolorsfori ndex(), or for a true colour
      > image loop through the pixels and usage imagecolorat().
      >
      > All the colours should have their red, green and blue components
      > equal, or near equal, if it's a greyscale image.[/color]

      Looping throught all pixels may become tediously sllloooooowwww for bigger
      pix. Why bother, just grayscale them regardless of previous state. Is that
      an option for you ?
      Pjotr


      Comment

      • Chung Leong

        #4
        Re: determine if image is grayscale

        "tjh" <t.hickey@ten.t sacmoc> wrote in message
        news:vl00d.1631 54$mD.122719@at tbi_s02...[color=blue]
        >
        > hi -
        >
        > i'm a bit new to PHP - i've been messing around with the GD2 image[/color]
        functions[color=blue]
        > and was wondering:
        >
        > i see a lot of information about converting a color image to grayscale but[/color]
        i[color=blue]
        > can't figure out how to determine whether an image (GIF or JPG) is[/color]
        grayscale[color=blue]
        > in the first place.
        >
        > is there a way to do this in PHP?
        >
        > thanks for any advice!
        >
        > tjh
        >[/color]

        For JPEG, you can call getimagesize() to get the number of color channels.
        If it's one, then the image is greyscale. For GIF, you can scan the palette.


        Comment

        Working...