pixel values of image changes after using imagesetpixel()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunnycs121
    New Member
    • Dec 2013
    • 1

    #1

    pixel values of image changes after using imagesetpixel()

    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):
    Code:
    <?php
    if (isset($_POST['go']))
    {
    	if ($_FILES["file"]["error"] > 0)
    	{
    		echo "Error: " . $_FILES["file"]["error"] . "<br>";
    		
    	}
    	else
    	{
    		$image_to_process = $_FILES["file"]["tmp_name"];
    		$im = imagecreatefromjpeg($image_to_process);
    		////////////++++++++++++++++++++++++++++++++++++++++++++++++////////////////
    		////-------IMAGE PIXEL MANIPULATION TILL CREATING ARRAY OF PIXLES-----------
    		{
    			$width =  imagesx($im);
    			$height = imagesy($im);
    			for($i=0;$i<$width;$i++)
    			{
    				for($j=0;$j<$height;$j++)
    				{
    					$color_index = imagecolorat($im, $i, $j);
    					$pix_arr[$i][$j] = $color_index;
    					
    				}
    			}
    			for($i=0;$i<$width;$i++){
    			for($j=0;$j<$height;$j++)
    			{
    				echo $pix_arr[$i][$j];
    				?>
                    <br/>
                    <?php
    			}
    			}
    			$img = imagecreatetruecolor($width,$height);
    			for ($i = 0; $i < $width; $i++)
    			{
    				for ($j = 0; $j < $height; $j++)
    				{
    					imagesetpixel($img, $i, $j, $pix_arr[$i][$j]);
    				}
    			}
    			imagejpeg($img, 'C:\wamp\www\testing\test.jpg');
    		}//anvi
    	}//else
    }
    ?>
    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.

    Code:
    <?php
    if (isset($_POST['go']))
    {
    	if ($_FILES["file"]["error"] > 0)
    	{
    		echo "Error: " . $_FILES["file"]["error"] . "<br>";
    		
    	}
    	else
    	{
    		$image_to_process = $_FILES["file"]["tmp_name"];
    		$im = imagecreatefromjpeg($image_to_process);
    		////////////++++++++++++++++++++++++++++++++++++++++++++++++////////////////
    		////-------IMAGE PIXEL MANIPULATION TILL CREATING ARRAY OF PIXLES-----------
    		{
    			$width =  imagesx($im);
    			$height = imagesy($im);
    			$total_pix = $width*$height;
    			for($i=0;$i<$width;$i++)
    			{
    				for($j=0;$j<$height;$j++)
    				{
    					$color_index = imagecolorat($im, $i, $j);
    					$pix_arr[$i][$j] = $color_index;
    					
    				}
    			}
    			for($i=0;$i<$width;$i++){
    			for($j=0;$j<$height;$j++)
    			{
    				echo $pix_arr[$i][$j];
    				?>
                    <br/>
                    <?php
    			}
    			}
    		}//anvi
    	}//else
    }
    ?>
    Why the values are different. they must be same.

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

    Attached is the sample image on which i am testing.

    I will be very thankful to you for your help.
    Attached Files
Working...