Can't save images using python ImageEnhance module

3.3k views Asked by At

I am a new python programmer and I am trying to use the ImageEnhance module to create a set of images that increase sharpness over time.

The script below works with one problem: the new images are being save to a temporary directory that quickly deletes the files.

How do I alter the save function to point to a proper directory.

Thanks in advance.

#import modules
import sys  
import os
import Image, ImageEnhance,

#find directory, load picture
os.chdir ("/Users/ericmaie/Desktop/test")
backup_directory = "/Users/harddrive/Desktop/test2"

counter = 1

while (counter < 5):

    ima = Image.open("1.png")
    enhancer = ImageEnhance.Sharpness(ima)
    factor = (counter + 10) / 4.0
    enhancer.enhance(factor).show("Sharpness %f" % factor)
    ima.save(str(counter) + '.jpg')

counter = counter + 1
3

There are 3 answers

3
piertoni On

Have you tryed something like this?

path="C:\\Somewhere\\myproperdirectory\\"
ima.save(path+str(counter) + '.jpg')
0
ninehundred On

You can save the modified image by replacing the 'show' with 'save'.

path="C:\\Your\\targetFolder\\"
enhancer.enhance(factor).save(path, quality=100)
0
kindall On

You are creating the enhanced version of the image:

enhancer.enhance(factor).show("Sharpness %f" % factor)

... then promptly throwing it away because it is never stored in a variable. Naturally it is not being saved, because you are not saving it.

Instead, store the enhanced image in its own variable, e.g.:

enhanced = enhancer.enhance(factor)
enhanced.show()
enhanced.save(str(counter) + ".jpg")

The .show() command does save the image, but only incidentally, because it can't hand it off to a viewer app without doing that. This temporary file is deleted as soon as possible, as you've discovered.