Search code examples
pythonopencvimage-processingjpegimage-compression

The images are different even though they are saved to the same quality


I used the function imwrite of OpenCV for Python in order to convert a set of images from PNG to JPEG format, without specifying the quality (the default value is 95), so I obtained a first set of JPEG images.

Then I used the same function in order to convert the same set of images from PNG to JPEG format, specifying the value 75 for quality, so I obtained a second set of JPEG images.

Finally, I tried to use the same function in order to convert the first set of JPEG images, specifying the value 75 for quality, so I obtained a third set of JPEG images.

When I perform a binary comparison between the second and third set of JPEG images, the images are different. Why? The target quality is the same (75), but the source images are different.


Solution

  • When you do your first compression (Quality 95) your first set of JPEGs are not going to be identical to the input PNGs when uncompressed. JPEG is lossy. That means the input to your Quality 75 compression is not the same. In the first case it is your original PNG images, and in the second case it is a JPEG distorted version of the same files. Since the input images are not the same, the output will not be the same either.