# Retrieve pixel values from the output jpeg image

I have this jpeg image (abc.jpg) and I change some pixel values of this image and store this on hard disk, to later see the changed the pixel values. The name of the output jpeg image is say, xyz.jpg. The problem here is in the image xyz.jpg. The changed pixels does not give me the exact values which I changed them to.

Example:

In abc.jpg at the pixel position (x,y)=(1,10) the pixel value is (231, 120, 120). Now, I want to change it to (230, 120, 120). Now, I save this output image as xyz.jpg. When I try to retrieve the values from position (1,10) I get the pixel value as (232, 120, 120). Now you see the problem, I can't get the value to which I have changed. I am doing all this in MATLAB. And yes, I have tried to save the output image as .tiff, .bmp, .gif. But the size of these formats is very large as compared to .jpg. Please suggest some solution to this problem.

Ask yourself: why do you think the TIFF and BMP files are so large while JPEG files are small?

Answer: JPEG is a lossy compression format and achieves small file size by removing details from your image imperceptible to the human eye. By design, JPEG cannot guarantee that the decompressed image will be pixel-by-pixel identical to the compressed image - it just preserves the overall content the human eye can perceive. You could try increasing the quality parameter, but this probably won't solve the problem in your case. Brightness levels will still be quantized, and color information downsampled.

Some "solutions":

• Do not use JPEG, use a lossless compression format.
• Do your image editing/modifications directly in the compressed domain. I doubt there are tools in Matlab to do that, and compressed-domain image modification is not as straightforward as writing into an array of pixels!
• Compressed Domain image modification would still not solve his problems because numerical errors will still make it less than ideal. (and one would still go through quantization even if you keep same $Q$). So best thing can only be of using lossless compression. – Dipan Mehta Feb 19 '12 at 9:16

JPEG is a lossy format, which means that noise will be added between compression and decompression. So, in general, you can't get a noiseless result (same pixels out as in for the entire image).

For individual pixels though, you might be able to force a desired value by dithering a region around a single pixel of interest. You might try many random low level noise patterns around the pixel of interest (it might involve up to the entire macroblock) by trial and error and see if one of the random patterns moves the result in the desired direction by the desired amount, or perhaps a genetic algorithm can converge on an appropriate dither pattern.

Whether or not the above works will likely depend on what quantization or compression level the JPEG compressor is configured for.

What I think JPG format file is one of the richest image file. When you change pixel values in PNG format file, the value is exactly being changed for that pixel, but the thing is when you try doing samething with jpg file it does not do so. You would better know by performing a simple experiment.

Make an image of $150*100$ in MSpaint. Put red color on first 50 rows, green on next 50 rows and blue from 100th to 150th row and make the last 50 rows half white and half black in color. like (1,150) to (50,175) white color, and (51,175) to (100,200) black color. then save this file in jpg format. and then open the saved image. You would notice something very interestng at the borders of the colors. You would get your answer.

• make image of 200*100 – Akash Subhani Apr 8 '12 at 4:43