+ Reply to Thread
Results 1 to 4 of 4

Thread: Grayscale Images with GD

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Grayscale Images with GD

    In computing, a grayscale or greyscale digital image is an image in which the value of each pixel is a single sample, that is, it carries the full (and only) information about its intensity. Images of this sort are composed exclusively of shades of neutral gray, varying from black at the weakest intensity to white at the strongest.
    In PHP, it is easy enough to do. First we want to create two image resources. First we need our original image so we can map its color. Second we need to create an image which will become our grayscale image.

    Code:
    <?php
    //change this to your image location
    $original = @imagecreatefrompng("/home/john/Documents/colorized.png")
        or die(
    "Cannot Initialize new GD image stream");


    $im = @imagecreate(imagesx($original), imagesy($original))
        or die(
    "Cannot Initialize new GD image stream");
    Next we want to create our color pallet. Since we our goal is a gray image,we will only use shades of black and white. Colorized images are generally within the RGB color space. Meaning each pixel is composed of red, green, and blue light which allows you to reproduce a broad array of colors. White is represented as 255,255,255 and black is represented as 0,0,0. Therefore, all gray shades are represented as x,x,x. So we thus create our pallet:
    Code:
    for ($i 0$i <= 255$i++) {
        
    $palette[$i] = imagecolorallocate($im$i$i$i);

    Next we want to create a function to convert from the RGB color space to some color space that only represents the pixels intensity. The YIQ is the best color space for this since the Y represents the luma of the pixel. The numbers I use in the function below comes from the Y component: YIQ - Wikipedia, the free encyclopedia
    Code:
    function grayscale($r$g$b) {
        return 
    0.199*$r 0.587*$g 0.114*$b;

    Now we actually need to get the RGB values of each pixel so we can convert if. To do this, we iterate over the entire image (so large images will take some time), and place the transformed pixel onto the new image we wish to create.

    Code:
    for($x 0$x imagesx($original); $x++) {
        for(
    $y 0$y imagesy($original); $y++) {
            
    $rgb imagecolorat($original$x$y);
            
    $r = ($rgb >> 16) & 0xFF;
            
    $g = ($rgb >> 8) & 0xFF;
            
    $b $rgb 0xFF;
            
    imagesetpixel($im$x$y$palette[grayscale($r$g$b)]);
        }

    Now all that is left is to set the header and create the image. The final code is below.

    Code:
    <?php
    header
    ("Content-type: image/png");

    $original = @imagecreatefrompng("/home/john/Documents/colorized.png")
        or die(
    "Cannot Initialize new GD image stream");


    $im = @imagecreate(imagesx($original), imagesy($original))
        or die(
    "Cannot Initialize new GD image stream");

    for (
    $i 0$i <= 255$i++) {
        
    $palette[$i] = imagecolorallocate($im$i$i$i);
    }

    function 
    grayscale($r$g$b) {
        return 
    0.199*$r 0.587*$g 0.114*$b;
    }

    for(
    $x 0$x imagesx($original); $x++) {
        for(
    $y 0$y imagesy($original); $y++) {
            
    $rgb imagecolorat($original$x$y);
            
    $r = ($rgb >> 16) & 0xFF;
            
    $g = ($rgb >> 8) & 0xFF;
            
    $b $rgb 0xFF;
            
    imagesetpixel($im$x$y$palette[grayscale($r$g$b)]);
        }
    }

    imagepng($im);
    imagedestroy($im);
    ?>
    Attached is an example of a colorized image (left) and the same image using the above code (right)
    Attached Thumbnails Attached Thumbnails Grayscale Images with GD-grayscale.png  
    Last edited by John; 10-18-2010 at 07:37 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Grayscale Images with GD

    Wow, very nice work!

  4. #3
    Join Date
    Sep 2008
    Posts
    20
    Rep Power
    0

    Re: Grayscale Images with GD

    ya its good

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Grayscale Images with GD

    Quote Originally Posted by adserverexpert View Post
    ya its good
    Thanks. I am glad you enjoyed it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 213
    Last Post: 04-14-2011, 07:57 PM
  2. Images
    By hbk in forum C and C++
    Replies: 4
    Last Post: 11-12-2010, 09:20 AM
  3. images from URL
    By prof.deedee in forum Java Help
    Replies: 1
    Last Post: 11-12-2009, 01:43 AM
  4. please help:put images together
    By sara933 in forum PHP Development
    Replies: 1
    Last Post: 05-24-2008, 08:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts