Good day,
I am trying to show that compressing an image many-many times (extremely many, as in 500 times), will show quality degradation. And from what I understand, this can happen when an image is saved many-many times in JPEG. I tried writing a MATLAB code to do this:
clc;close all;clear;
for i = 1:500
if i==1
a = imread('e:\ismoka_small.jpg');
currFileName = 'e:\multipleJpegs\001.jpg';
else
a = imread(currFileName);
if i <= 10
zeross = '00';
elseif i <= 100
zeross = '0';
elseif i <= 1000
zeross = '';
end
currFileName = ['e:\multipleJpegs\' zeross num2str(i-1) '.jpg'];
end
imwrite(a, currFileName, 'jpeg');
end
The end result however shows that no degradation occurs, and all the 500 images have the same file size. I was wondering if anyone can help me and explain why this is the case? Or do I have it wrong regarding the JPEG algorithm? Thanks in advance :)
The loss caused by JPEG compression is due to quantization, which is essentially rounding or truncation. If you're always saving with the same quality setting, it's quite possible that the quantization process produces the same results each time, especially if you do this many times - the pixels will degrade until they reach a point where they survive the round trip, then they won't change any more.
If you make any changes to the image before resaving, those changes will cause degradation in the 8x8 or 16x16 region where the changes were made.
If you save at different quality settings every time, you'll get different quantization each time and the image will definitely degrade, even sometimes if you use a higher setting.
Here I've repeated the test with my own image, using Python's PIL to open and save the image 100 times. I also opened and saved it one more time, to see if there was any additional degradation - there was no difference. I've resaved the JPEGs as PNG to prevent any further losses from StackOverflow's image engine.
Although there are measureable differences between the first and 100th saves, they are insignificant in comparison to the difference between the original and the first.
Original:
First save:
100th save: