This question is mostly related to jpeg compression and expected results when you read and save the same jpeg file several times.
Let's say you have read an uncompressed image (originally it has never been compressed). Then read and save it with a jpeg extension in OpenCV. As expected the jpeg file size becomes smaller (uncompressed: 4607 kB, jpeg: 314 kB). Then you read this saved jpeg image in OpenCV again and save it as a jpeg again. When you repeat this loop, what should we expect about the saved image file size, and individual pixel values? In OpenCV, there is this parameter about the JPEQ quality, as shown below.
cv2.imwrite("img.jpg", img, [cv2.IMWRITE_JPEG_QUALITY, 50])
When I repeatedly read the jpeg image and save it as jpeg again, with the same JPEQ quality parameter, it looks like the saved image file sizes are not changing. When I look at the differences between the previously saved image and the current image, there are pixel differences in some of the regions and especially at the bottom side, and right side of the image.
I know a little bit about jpeg compression. I know that it uses DCT on 8x8 blocks, quantize them, and then does an IDCT. Maybe it is because of the quantization, but shouldn't we expect that every saved jpeg image file size should be shrunk even a little bit. Also, what is going on at the bottom and right side of the image (Please see the difference image below)?
import cv2
import matplotlib
img_name = 'image_'
jpg_quality = 50
img = cv2.imread('uncompressed_image.png')
for i in range(10):
if i>0:
path = img_name + str(i)+'.jpg'
img = cv2.imread(path)
new_image_path = img_name + str(i+1)+'.jpg'
cv2.imwrite(new_image_path, img, [int(cv2.IMWRITE_JPEG_QUALITY), jpg_quality])
if i>0:
f, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
i1 = ax1.imshow(img_orig)
ax1.title.set_text('RGB')
diff = img - cv2.imread(new_image_path)
i2 = ax2.imshow(diff)
ax2.title.set_text('DIFFERENCE')
plt.show()