'Image inconsistency between saved and memory version

I have the following code. It seems like img_1 and img_2 are not the same. Looking at the pixels, I can tell there is a small diff, which can be hardly seen, but becomes a big deal later. Why does it happen and how to read the version of img_2 directly from s3?

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import boto3
import io
from PIL import Image, ImageChops

        
s3 = boto3.resource('s3', region_name='us-east-1')
bucket = s3.Bucket(bucket)
key = "some_key.jpeg"
object = bucket.Object(key)
response = object.get()
file_stream = response['Body']
img_1 = Image.open(file_stream)

img_1.save('/tmp/img1.jpeg')
img_2 = Image.open('/tmp/img1.jpeg')
diff = ImageChops.difference(img_1, img_2)
    
if diff.getbbox():
   print("images are different")
else:
   print("images are the same")


Solution 1:[1]

Probably because of a quality factor? Perhaps the default save() option uses a quality less than the original?

What about when you save using img_1.save('/tmp/img1.jpeg', quality="maximum")

More information on JPEG image quality in PILLOW here: https://pillow.readthedocs.io/en/stable/reference/JpegPresets.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1