I writing a Python app where I need to do some image tasks.

I'm trying PIL and it's ImageOps module. But it looks that the unsharp_mask method is not working properly. It should return another image, but is returning a ImagingCore object, which I don't know what is.

Here's some code:

import Image
import ImageOps

file = '/home/phius/test.jpg'
img = Image.open(file)
img = ImageOps.unsharp_mask(img)
#This fails with AttributeError: save
img.save(file)

I'm stuck on this.

What I need: Ability to do some image tweeks like PIL's autocontrast and unsharp_mask and to re-size, rotate and export in jpg controlling the quality level.

有帮助吗?

解决方案

What you want is the filter command on your image and the PIL ImageFilter module[1] so:

import Image
import ImageFilter

file = '/home/phius/test.jpg'
img = Image.open(file)
img2 = img.filter(ImageFilter.UnsharpMask) # note it returns a new image
img2.save(file)

The other filtering operations are part of the ImageFilter module[1] as well and are applied the same way. The transforms (rotation, resize) are handled by calling functions[2] on the image object itself i.e. img.resize. This question addresses JPEG quality How to adjust the quality of a resized image in Python Imaging Library?

[1] http://effbot.org/imagingbook/imagefilter.htm

[2] http://effbot.org/imagingbook/image.htm

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top