By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

pixel values of image changes after using imagesetpixel()

P: 1
I am simply taking the pixels values from jpeg image using the function imagecolorat() and then creating a jpg image back again by giving those pixel values to imagesetpixel(). the name of new image is "test.jpg". When i again extract the pixel values from this test.jpg i found that the extracted pixel values are different from those i gave to imagesetpixel().

This is the code which is taking an image (jpg), saving pixel values in 2d array "pix_arr[][]" and then creating new image (test.jpg):
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (isset($_POST['go']))
  3. {
  4.     if ($_FILES["file"]["error"] > 0)
  5.     {
  6.         echo "Error: " . $_FILES["file"]["error"] . "<br>";
  7.  
  8.     }
  9.     else
  10.     {
  11.         $image_to_process = $_FILES["file"]["tmp_name"];
  12.         $im = imagecreatefromjpeg($image_to_process);
  13.         ////////////++++++++++++++++++++++++++++++++++++++++++++++++////////////////
  14.         ////-------IMAGE PIXEL MANIPULATION TILL CREATING ARRAY OF PIXLES-----------
  15.         {
  16.             $width =  imagesx($im);
  17.             $height = imagesy($im);
  18.             for($i=0;$i<$width;$i++)
  19.             {
  20.                 for($j=0;$j<$height;$j++)
  21.                 {
  22.                     $color_index = imagecolorat($im, $i, $j);
  23.                     $pix_arr[$i][$j] = $color_index;
  24.  
  25.                 }
  26.             }
  27.             for($i=0;$i<$width;$i++){
  28.             for($j=0;$j<$height;$j++)
  29.             {
  30.                 echo $pix_arr[$i][$j];
  31.                 ?>
  32.                 <br/>
  33.                 <?php
  34.             }
  35.             }
  36.             $img = imagecreatetruecolor($width,$height);
  37.             for ($i = 0; $i < $width; $i++)
  38.             {
  39.                 for ($j = 0; $j < $height; $j++)
  40.                 {
  41.                     imagesetpixel($img, $i, $j, $pix_arr[$i][$j]);
  42.                 }
  43.             }
  44.             imagejpeg($img, 'C:\wamp\www\testing\test.jpg');
  45.         }//anvi
  46.     }//else
  47. }
  48. ?>
  49.  
Here is the code through which i am getting the pixel values of created test.jpg. but the pixel values are different from those i set in above code.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (isset($_POST['go']))
  3. {
  4.     if ($_FILES["file"]["error"] > 0)
  5.     {
  6.         echo "Error: " . $_FILES["file"]["error"] . "<br>";
  7.  
  8.     }
  9.     else
  10.     {
  11.         $image_to_process = $_FILES["file"]["tmp_name"];
  12.         $im = imagecreatefromjpeg($image_to_process);
  13.         ////////////++++++++++++++++++++++++++++++++++++++++++++++++////////////////
  14.         ////-------IMAGE PIXEL MANIPULATION TILL CREATING ARRAY OF PIXLES-----------
  15.         {
  16.             $width =  imagesx($im);
  17.             $height = imagesy($im);
  18.             $total_pix = $width*$height;
  19.             for($i=0;$i<$width;$i++)
  20.             {
  21.                 for($j=0;$j<$height;$j++)
  22.                 {
  23.                     $color_index = imagecolorat($im, $i, $j);
  24.                     $pix_arr[$i][$j] = $color_index;
  25.  
  26.                 }
  27.             }
  28.             for($i=0;$i<$width;$i++){
  29.             for($j=0;$j<$height;$j++)
  30.             {
  31.                 echo $pix_arr[$i][$j];
  32.                 ?>
  33.                 <br/>
  34.                 <?php
  35.             }
  36.             }
  37.         }//anvi
  38.     }//else
  39. }
  40. ?>
  41.  
Why the values are different. they must be same.

I think there is the problem in imagecreatetruecolor() or imagesetpixel().

Attached is the sample image on which i am testing.

I will be very thankful to you for your help.
Attached Images
File Type: jpg img04.jpg (333 Bytes, 69 views)
Dec 7 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.