We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › JPG Image Deterioration Sketch
Page Index Toggle Pages: 1
JPG Image Deterioration Sketch (Read 3142 times)
JPG Image Deterioration Sketch
Jun 17th, 2010, 8:20am
I'm playing around with a sketch that loads a jpg image, then saves it again as a jpg, then loads the saved jpg, and resaves it as a jpg etc. Why?! Well, I was curious to see how much information gets compressed in a lossy format when constantly resaving an image. I've seen that when iterating over the cycle the first time, there's a noticeable change in image quality - but after that the image filesize remains the same and there is no discernible difference between the 3rd and the 503rd image resaved.

So has the sun got to my brain and I've written an error into my code (which is likely), or can I not recompress the jpg more than once??? Or is here something more silly I haven't thought about (also quite likely)?  Huh

Here's my code:
int openFile = 1; //name the source image as 1.jpg
int saveFile = 2;
int x, y=0;
PImage img;

void setup(){
 size(386, 600);//image size

void draw(){

void mousePressed(){
 //can also paste the following code into draw for automation
 img = loadImage(openFile+".jpg");
 image(img, x, y, img.width, img.height);
 openFile = saveFile;
 saveFile = saveFile+1;

 print("   saving:");

 if (saveFile>1000){

Re: JPG Image Deterioration Sketch
Reply #1 - Jun 17th, 2010, 9:44am
if i remember correctly a image can only be compress "that much".

I've personally only made loss-less compression programs but i think the same "logic" applys to lossy compression so here's my go at explaining it:
If you recompress the same file using the exact same compression method over and over again you will eventually hit a sweet-spot where trying to compress the file one more time will just result in the exact same file. This is atleast true for loss-less compression.
But if i recall correctly jpg compression slices the image into equal sized small squares where the size of the squares controll the final quality of the image and also how much information that is lost (on a side note, when you see jpg artifacts it is infact these square you see if you look closely). The program/algorithm then goes onto compressing eatch square individually by calculating some average color and doing some magic resulting in this square being able to de explained with a less amount of information than before, im not really to educated on the specifics on how this works in jpg compression.
Anyways if im guessing correctly compressing the image with the same square-size will eventually result in every square being recompressed into itself and therefor resulting in the same image.

But im not 100% sure of all this, so don't quote me on it...
Re: JPG Image Deterioration Sketch
Reply #2 - Jun 17th, 2010, 12:20pm
The reason for this is because the second time around saving that same image, it has already gone through the quantization process and will perfectly compress again without loss of information.. all the cosine's match up...

Try randomly slightly changing the color of pixels here and there, which adds more information to the image.. then it will lose more of the original information with each save.

Re: JPG Image Deterioration Sketch
Reply #3 - Jun 17th, 2010, 1:28pm
Yes, I intuitively guessed the same...
The danger in using a lossy format is with multiple saves after small changes (adjust color, crop a bit, retouch there, etc.).
Re: JPG Image Deterioration Sketch
Reply #4 - Jun 17th, 2010, 7:30pm
Yeah, it would be worse to adjust image saturation & save, than to make localized edits though because as guss mentioned, the format uses small squares to compress the image data, though I don't believe those squares could change size. My understanding was that they're always 8 by 8 pixels.

I know all this is outlined in tons of detail on wikipedia (http://en.wikipedia.org/wiki/JPEG#Encoding), but to shed a little more light on the process and refresh my own memory, here's a simplified explanation:

The image is first transformed into HSB color space (also known as Y Cb Cr), and downsampled into integer values allotting more bits for the brightness component. Then it's divided up into those 8 by 8 blocks, so you get nice localized arrays of 64 pixels per block.

These arrays are then fed through a discreet cosine transform, (one per channel I think) and you end up with 64 scalar values per channel of all the cosines that make up that 64 pixel block. This is the insane mathematics portion -- it can be the most difficult part to understand, but there are a ton of examples explaining how DCT's work online, and even code samples showing how they're written. The most advantageous thing about DCT's is the result is sorted from low to high frequency. This is nice because it makes it easy to choose the frequencies you think matter most for your compression, in this case it's the lower frequency information as it's what your eyes see more of at a distance.

Next, the result is fed through a low pass filter which is the big lossy component of the process. In this context it is called Quantization. All that really happens here is you divide the entire array by a scalar and round everything to integer values. This brings most of the high frequency information to 0 which you can throw away, and turns the low frequency information to small scalars that are easy to compress.

The last step is a combination of run-length encoding and huffman coding to compress those integer values. RLE & Huffman are both loss-less compression techniques.

It's really not all that difficult to write the algorithm as long as you use test-driven-development and build it one step at a time. And if one was going to attempt it, it's probably worth getting RLE and Huffman working first as those are a considerable amount of the work on thier own. =)

Wow, so yeah I'm like totally off topic but maybe I helped a couple people out with their coursework Wink

Re: JPG Image Deterioration Sketch
Reply #5 - Jun 18th, 2010, 2:30am
Enlightening, thanks for the replies!

Looks like what I wanted to do may not work the way I intended - essentially I wanted to reduce an image down to highly visible artifacts and "glitches" by reproducing it many times over. Hmmm...
Page Index Toggle Pages: 1