0

I'm attempting to write a program that will take an image file I input, save a new compressed version at 50% its original quality, then open the new file and run again n number of times. Essentially I need it to be a huge compression feedback loop that creates a new file each time. It's for a visual art project.

Unfortunately it doesn't look like PIL will recompress files that were created using its own compression algorithm, so basically when I try to run it I end up with n number of the same exact file.

I'm using PIL and Python 3.3 on an Intel Mac running OS X 10.7. This is the entire program:

import os
from PIL import Image

def compressLoop(infile, times):
    '''
    Progressively loads, compresses, creates files based on original JPEG 'times' number of times.
    '''
    n = 1
    baseName, e = os.path.splitext(infile)
    try:
        while n <= times:
            f, e = os.path.splitext(infile)
            f = (baseName + str(n))
            outfile = f + ".jpg"
            #open previously generated file
            compImg = Image.open(infile)
            #compress file at 50% of previous quality
            compImg.save(outfile, "JPEG", quality=50)
            infile = outfile
            n = n+1
    except IOError:
        print("Cannot convert", infile)

def main():
    infile = str(input("Filename to compress: "))
    times = int(input("Times to process: "))
    compressLoop(infile, times)

main()

Are there any workarounds to this problem? Am I using the right function in compImg.save(outfile, "JPEG", quality=50) or is there another way to compress image files?

Thanks in advance for the help!

  • Why can't you just divide 100 by 2 n times and use that for the quality? – mgilson Nov 27 '13 at 00:36
  • I tried that but the quality argument only takes integers so I hit the floor pretty quickly. – user3023883 Nov 27 '13 at 00:43
  • 1
    JPEG Quality 50 doesn't mean 50% of the quality of last time, it means 50% of the best possible quality. So, decompressing and recompressing at 50% over and over will have only minor effects. – abarnert Nov 27 '13 at 00:55
  • So in order to do what I'm looking to I would need to somehow mark the compressed file as the best quality, correct? Is there a way to do that? – user3023883 Nov 27 '13 at 01:08
  • @user3023883: No, you're missing the point. "The best quality" means "the best possible quality for the JPEG algorithm for this input". There's no marking anything; it's got the decompressed bits, and you're telling it to do about half as best as it can with those bits. In short, what you're trying to do doesn't make any sense. – abarnert Nov 27 '13 at 01:13
  • This smells very strongly of an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What exactly are you trying to do here? If you just want to explore the effects of repeated JPEG compression at quality=50, you've done that successfully, and you've learned that it's more than the effects of doing a single quality=50 compression, but not _that much_ more. If you want to see quality=50, 25, 13, 7, 4, 2, and 1, then just do that. If you want something that looks like a more extreme version of JPEG quality=50 compression artifacts, you're barking up the wrong tree. – abarnert Nov 27 '13 at 01:18
  • Ok, well I wasn't sure before how JPEG compression worked, now I am. I want a series of n number of images that appear to gradually deteriorate in quality, regardless of the actual mechanisms of compression at work behind it. I'm new to programming and I come from an arts background, so I'm a little out of my depth. I thought this solution would work based on the little I knew about jpegs, I'll look elsewhere. – user3023883 Nov 27 '13 at 01:49

0 Answers0