Skip to content

Instantly share code, notes, and snippets.

@OrganicIrradiation
Created March 23, 2015 09:41
Show Gist options
  • Save OrganicIrradiation/349db2ddd83213e55437 to your computer and use it in GitHub Desktop.
Save OrganicIrradiation/349db2ddd83213e55437 to your computer and use it in GitHub Desktop.
Effect of repeated JPEG compression on image quality and content
% Load the 'original image', rescale to 1920 width, crop to 1080 height
originalImage = imread('YOURPHOTOHERE.JPG');
scaledImage = imresize(originalImage, [NaN 1920]);
scaledImage = imcrop(scaledImage,[0, size(scaledImage,1)/2-1080/2,...
size(scaledImage,2), size(scaledImage,1)/2+1080/2-1]);
% Create a data output folder
[currentPath, ~, ~]= fileparts(mfilename('fullpath'));
mkdir(fullfile(currentPath,'data')) % Create the data directory
% Preallocate the filesize array
filesizes = zeros([101,1]);
for q = 100:-1:0
% Save the compressed image
filename = sprintf('frame%03d.jpg',100-q);
filepath = fullfile(currentPath,'data',filename);
imwrite(scaledImage,filepath,'Quality',q);
s = dir(filepath);
filesizes(100-q+1) = s.bytes;
figure(1)
plot(1:101,filesizes,'LineWidth',5);
text(20,2*max(filesizes)/3,sprintf('Image Quality: %03d\nSize: %u bytes',q,filesizes(100-q+1)),...
'FontSize',36)
xlim([1,101])
plotImage = getframe;
% Load the compressed image and overlay a plot of the filesizes
compressedImage = imread(filepath);
fusedImage = imfuse(compressedImage,plotImage.cdata,'blend');
imwrite(fusedImage,filepath,'Quality',100);
end
filename = 'decreasingimagequality.avi';
filepath = fullfile(currentPath,'data',filename);
writerObj = VideoWriter(filepath);
writerObj.FrameRate = 10;
open(writerObj);
for i = 0:100
filename = sprintf('frame%03d.jpg',i);
filepath = fullfile(currentPath,'data',filename);
frame = im2frame(imread(filepath));
writeVideo(writerObj,frame);
end
close(writerObj);
% Load the 'original image', rescale to 1920 width, crop to 1080 height
originalImage = imread('YOURPHOTOHERE.JPG');
scaledImage = imresize(originalImage, [NaN 1920]);
scaledImage = imcrop(scaledImage,[0, size(scaledImage,1)/2-1080/2,...
size(scaledImage,2), size(scaledImage,1)/2+1080/2-1]);
% Create a data output folder
[currentPath, ~, ~]= fileparts(mfilename('fullpath'));
mkdir(fullfile(currentPath,'data')) % Create the data directory
% Preallocate the filesize array
filesizes = zeros([101,1]);
% Save highest quality image
filename = 'frame000.jpg';
filepath = fullfile(currentPath,'data',filename);
imwrite(scaledImage,filepath,'Quality',100);
for q = 99:-1:0
% Load previous compressed image
filename = sprintf('frame%03d.jpg',100-q-1);
filepath = fullfile(currentPath,'data',filename);
scaledImage = imread(filepath);
% Save the compressed image
filename = sprintf('frame%03d.jpg',100-q);
filepath = fullfile(currentPath,'data',filename);
imwrite(scaledImage,filepath,'Quality',q);
end
filename = 'repeatedcompression.avi';
filepath = fullfile(currentPath,'data',filename);
writerObj = VideoWriter(filepath);
writerObj.FrameRate = 10;
open(writerObj);
for i = 0:100
filename = sprintf('frame%03d.jpg',i);
filepath = fullfile(currentPath,'data',filename);
frame = im2frame(imread(filepath));
writeVideo(writerObj,frame);
end
close(writerObj);
% Create a data output folder
[currentPath, ~, ~]= fileparts(mfilename('fullpath'));
mkdir(fullfile(currentPath,'data')) % Create the data directory
% Preallocate the filesize array
filesizes = zeros([101,1]);
% Save highest quality image
filename = 'frame000.jpg';
filepath = fullfile(currentPath,'data',filename);
imwrite(scaledImage,filepath,'Quality',100);
for q = 1:1000
% Load previous compressed image
filename = sprintf('frame%03d.jpg',q-1);
filepath = fullfile(currentPath,'data',filename);
scaledImage = imread(filepath);
% Save the compressed image with random (0-25) quality
filename = sprintf('frame%03d.jpg',q);
filepath = fullfile(currentPath,'data',filename);
imwrite(scaledImage,filepath,'Quality',mod(100-q,100));
end
filename = 'repeatedcompression10x.avi';
filepath = fullfile(currentPath,'data',filename);
writerObj = VideoWriter(filepath);
writerObj.FrameRate = 30;
open(writerObj);
for i = 0:1000
filename = sprintf('frame%03d.jpg',i);
filepath = fullfile(currentPath,'data',filename);
frame = im2frame(imread(filepath));
writeVideo(writerObj,frame);
end
close(writerObj);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment