Jump to content

output problem


piyush_v

Recommended Posts

trying to make a script that interprets an image and reads the rgb values of each pixel. Ive come up with the code tht can interpret the values with help from my friends but i have no idea asto how to print it out. the format is supposed to be

Pixel R G B

 

and example line would be

 

120X200 120 230 240

the code till now is

<?php 
$source_file = "test_image.jpg";



$im = ImageCreateFromJpeg($source_file); 

$imgw = imagesx($im);
$imgh = imagesy($im);

// n = total number or pixels

$n = $imgw*$imgh;



for ($i=0; $i<$imgw; $i++)
{
for ($j=0; $j<$imgh; $j++)
{

	// get the rgb value for current pixel

	$rgb = ImageColorAt($im, $i, $j); 

	// extract each value for r, g, b

	$r = ($rgb >> 16) & 0xFF;
	$g = ($rgb >>  & 0xFF;
	$b = $rgb & 0xFF;



}
}


?>

 

any help will be appreciated

thanks

Link to comment
Share on other sites

Hi

 

This should do it:

 

<?php 
$source_file    = "test_image.jpg";
$im             = ImageCreateFromJpeg($source_file); 
$imgw           = imagesx($im);
$imgh           = imagesy($im);
// n = total number or pixels
$n              = $imgw*$imgh;

for ($i=0; $i<$imgw; $i++)
{
        for ($j=0; $j<$imgh; $j++)
        {
            // get the rgb value for current pixel	
            $rgb = ImageColorAt($im, $i, $j);		
            // extract each value for r, g, b		
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >>  & 0xFF;
            $b = $rgb & 0xFF;

            echo $i . "x" . $j . " " . $r . " "  . $g . " " . $b . "<br />";	
        }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.