SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to "get" number of pixels in an image?

    Hello,
    I am relatively new to php coding, and I have a question. Is there any way to find the number of, say, black pixels in an image with php?

  2. #2
    SitePoint Wizard bronze trophy JRMillion's Avatar
    Join Date
    Apr 2004
    Location
    Arlington VA
    Posts
    2,094
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use GD, like this

    PHP Code:
    $img ImageCreateFromJpeg("image.jpg");

    $rgb ImageColorAt($img$x,$y)

       
    $r = ($rgb >> 16) & 0xFF;
       
    $g = ($rgb >> 8) & 0xFF;
       
    $b $rgb 0xFF;
       echo 
    'red '.$r .' green '.$g.' blue '.$b;


    this would give you the color of the pixel at $x,$y
    you could use this type of code along with the other GD functions.

  3. #3
    SitePoint Enthusiast ssx-gun's Avatar
    Join Date
    Sep 2002
    Location
    Strongsville, OH
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could throw in a loop going through each pixel in the image if you really hard to with the above
    PHP: Pills Help People
    ---
    weird-one.com


  4. #4
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mind you, with jpeg-compression a "black" area won't be exactly "black", meaning it won't be exactly #000000 . It would end up with some numbers changed, so you would have a hard time finding pixels that are black, but not really black.

  5. #5
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JRMillion
    you can use GD, like this

    PHP Code:
    $img ImageCreateFromJpeg("image.jpg");

    $rgb ImageColorAt($img$x,$y)

       
    $r = ($rgb >> 16) & 0xFF;
       
    $g = ($rgb >> 8) & 0xFF;
       
    $b $rgb 0xFF;
       echo 
    'red '.$r .' green '.$g.' blue '.$b;


    this would give you the color of the pixel at $x,$y
    you could use this type of code along with the other GD functions.
    This is a very good code, but I was looking for how to find the number of actual pixels in the image, I already knew how to find the color. I just can't figure out how to loop the damn thing! Thanks for trying, though

  6. #6
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need the size of the image, which you can get with getimagesize() . Then you loop through the image with replacing $x and $y in your call to ImageColorAt() with the position of the pixel you are currently at.

  7. #7
    SitePoint Wizard bronze trophy JRMillion's Avatar
    Join Date
    Apr 2004
    Location
    Arlington VA
    Posts
    2,094
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excactly

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $x imagesx;
    $y imagesy;

    $NumPix = ($x $y);

    echo 
    $NumPix
    or round $x * $y for no decimals.


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
  •