Re: How to improve rotation quality?
Christopher Barker <[email protected]>
| Newsgroups | gmane.comp.python.image |
|---|---|
| Message-ID | <[email protected]> |
Alec Bennett wrote: > I'm wondering if anyone has any idea how to get better results from > PIL's rotate() function? No matter what filter I use I'm getting very > jagged edges after rotating an image. > > Here's how I'm invoking the filters: > > pic = pic.rotate(random_rotation, resample=Image.NEAREST, expand=1) > pic = pic.rotate(random_rotation, resample=Image.BILINEAR, expand=1) > pic = pic.rotate(random_rotation, resample=Image.BICUBIC, expand=1) # > the best I think Too bad ANTIALIAS doesn't seem to be available for rotate -- darn. It might look better. But to some extent, you can only get so good rotating an image. However, I took at look at your test images -- it looked like better smoothing was going on inside the white border than outside, which made me think -- when rotating, if you are going to use interpolation, how does it interpolate to outside the image? It doesn't. So I tried adding a black background to the image first, then rotating it -- much better. See the enclosed version of your test code, and a rotated image. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception [email protected] _______________________________________________ Image-SIG maillist - [email protected] http://mail.python.org/mailman/listinfo/image-sig
test.py
(application/x-python, 961 B)
from PIL import Image
im1 = Image.open("test.jpg").convert('RGBA')
size = im1.size
im = Image.new("RGBA", (size[0]+20, size[1]+20), "black" )
im.paste(im1, (10,10))
# http://www.pythonware.com/library/pil/handbook/image.htm
# NEAREST (use nearest neighbour),
# BILINEAR (linear interpolation in a 2x2 environment)
# BICUBIC (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode "1" or "P", it is set to NEAREST.
# make different versions of the image using different filters
angle = 5
im1 = im.rotate(angle, expand = 1)
im2 = im.rotate(angle, resample=Image.NEAREST, expand = 1)
im3 = im.rotate(angle, resample=Image.BILINEAR, expand = 1)
im4 = im.rotate(angle, resample=Image.BICUBIC, expand = 1)
images = [im1, im2, im3, im4]
#save each of the images as test-1.jpg, test-2.jpg, etc.
for counter, i in enumerate(images):
i.save("test-" + str(counter) + ".jpg", dpi=(300, 300))
test-3.jpg
(image/jpeg, 42 KB) - not displayed