How to improve rotation quality?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to improve rotation quality?

pontissimo
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

I posted a sample app that opens an image and rotates it using each of the filters. You can see the jagged edges pretty plainly.

I have a slideshow app that I'm working on where these edges are very obvious, so I'm wondering if anyone might have any tips on getting better results from rotate(), or from some other method or module that might do a better job?

My very simple sample app that tests each of the filters is here:

http://sinkingsensation.com/stuff/rotation_test.zip



_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: How to improve rotation quality?

Edward Cannon-2
Try using Image.ANTIALIAS, I find it has the best quality.

On Wed, Feb 17, 2010 at 3:30 AM, Alec Bennett <[hidden email]> 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
>
> I posted a sample app that opens an image and rotates it using each of the
> filters. You can see the jagged edges pretty plainly.
>
> I have a slideshow app that I'm working on where these edges are very
> obvious, so I'm wondering if anyone might have any tips on getting better
> results from rotate(), or from some other method or module that might do a
> better job?
>
> My very simple sample app that tests each of the filters is here:
>
> http://sinkingsensation.com/stuff/rotation_test.zip
>
>
>
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: How to improve rotation quality?

Chris Barker
In reply to this post by pontissimo
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

[hidden email]

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig

test.py (1K) Download Attachment
test-3.jpg (57K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to improve rotation quality?

pontissimo
> 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.

Wow, what a difference! Even if the border is a single pixel, much better results. I set the border color to grey, which seems to work as well as black, and am now a happy slideshow maker.

Huge thanks.

And to anyone else coming down this road, if the resampling filters aren't having any effect, update to the latest PIL.








_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: How to improve rotation quality?

Chris Barker
Alec Bennett wrote:
>  > 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.
>
> Wow, what a difference! Even if the border is a single pixel, much
> better results. I set the border color to grey, which seems to work as
> well as black, and am now a happy slideshow maker.

Ideally, you should use whatever color is going to be in the background
when you show the image.

Even better would be if you could blend it with a transparent
background, and show it that way, but that would require an alpha
channel, and I don't think jpeg supports that.

Glad it worked for you,

-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

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: How to improve rotation quality?

pontissimo
> Ideally, you should use whatever color is going to be in the background when you show the image.

Agreed, but the problem is I'm making a collage of pictures where the new pictures are placed above previous pictures, so there's no one consistent background color.

> Even better would be if you could blend it with a transparent background, and show it that way,
> but that would require an alpha channel, and I don't think jpeg supports that.

I'm converting to BMP and assigning an alpha channel so this is indeed a possibility. Another thought I had was making a drop shadow effect, which the thin gray edge outside of my white border already approximates.

Interestingly, when I've tried making a transparent canvas in PIL (which I'd have to do to add an alpha border) in the past, I consistently got weird artifacts in the transparent areas. The workaround I used was to make a small blank PNG that was pure alpha and resize it as necessary.

If I test out the alpha border I'll post back to this thread.





_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig