ANTIALIAS vs BICUBIC in PIL(Python Image Library)?

12,913

Solution 1

These are listed in order of lowest to higest complexity. There will be visual differences between them. The main difference will be how long the algorithm takes to execute.

You'll have to decide what matters more to you, speed, or quality. If you're only doing 5 images, go for quality. If you're doing 100,000 images, maybe go for speed. It really depends on what you're using it for.

The 2x2 and 4x4 environment means that the algorithm looks at a 2x2 or 4x4 area of pixels.

Solution 2

ANTIALIAS is no longer the proper term, it was replaced by LANCZOS which is a more descriptive term for the algorithm used. You can still use ANTIALIAS in your code for backward compatibility purposes but it's not recommended.

LANCZOS uses a larger pattern than BICUBIC and should produce slightly sharper results. It will also be slower.

The documentation has been changed since the question was asked, and the references to 2x2 or 4x4 have been removed. You probably weren't the only one confused by them.

resample – An optional resampling filter. This can be one of PIL.Image.NEAREST
           (use nearest neighbour), PIL.Image.BILINEAR (linear interpolation),
           PIL.Image.BICUBIC (cubic spline interpolation), or PIL.Image.LANCZOS (a high-quality
           downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set
           PIL.Image.NEAREST.

The below is no longer valid, it was fixed in Pillow 2.7. I'm leaving it here for those with older versions, although I'd highly advise you to upgrade.


I've now gone through the source to figure out the details. I'm not terribly pleased by what I saw.

First, BICUBIC. There are a number of formulas which can be classified as bicubic, the most common of these being the Catmull-Rom interpolation. That's not what PIL uses. Don Mitchell and Arun Netravali wrote a paper that analyzes all the variations and characterizes them using two variables B and C; the one used by PIL corresponds to B=0 and C=1. In the Mitchell-Netravali paper this is clearly in the Ringing artifact region. This means that enlarged images will have unnatural bright or dark halos around edges.

Next up is ANTIALIAS. This is based on a Lanczos-3 filter, which would ordinarily be a good choice for both downsizing and upsizing. Unfortunately there's a bug in the code when upsizing - rather than taking in an area of 6x6 pixels to calculate the result, it's truncated at 2x2 pixels. This makes it barely better than bilinear.

Share:
12,913
xunzhang
Author by

xunzhang

OCD

Updated on June 05, 2022

Comments

  • xunzhang
    xunzhang almost 2 years

    I am using PIL to resize my images, my case is to scale up the original image.

    I am confused about the algorithm used with `resample=ANTIALIAS'.

    According to the document below, ANTIALIAS seems to be the best while scaling down. I wonder In which case can BICUBIC win?(some of my test case shows bicubic is better choice)

    An optional resampling filter. 
      This can be one of NEAREST (use nearest neighbour), 
      BILINEAR (linear interpolation in a 2x2 environment), 
      BICUBIC (cubic spline interpolation in a 4x4 environment), 
      or ANTIALIAS (a high-quality downsampling filter). 
    If omitted, or if the image has mode “1” or “P”, it is set NEAREST.
    

    I am also confused about the linear interpolation in a 2x2 environment and cubic spline interpolation in a 4x4 environment in the document. What does it mean here?

    Thanks.

  • xunzhang
    xunzhang about 10 years
    in some of my test case, bicubic is the best. can u explain that?
  • aglassman
    aglassman about 10 years
    They all have their strengths and weaknesses, it really depends on the source image. Some algorithms may look better for images with sharp contrast lines, others may work better for natural scenes.
  • aglassman
    aglassman about 10 years
    Just do some google searching. There is a lot of blog posts and other things out there on which algorithms work best for which types of images. In the end though, it comes down to what looks better to you.
  • Mark Ransom
    Mark Ransom about 10 years
    The problem is that while "bicubic" is a well defined algorithm, "antialias" is not. It's impossible to find a blog post which will define the difference. The algorithm used by PIL is probably well known, just not by that name. I've never tried to dig into the source to figure it out for myself though.
  • princ_sameer
    princ_sameer over 6 years
    Did you report theses issues to PIL? and regardless do you happen to know if they are still issues?
  • Mark Ransom
    Mark Ransom over 6 years
    @GordonWrigley No and no. I suspect with active development on Pillow that things have improved.
  • user666
    user666 over 5 years
    Looks like the ANTIALIAS filter has been renamed to LANCZOS, and the release notes say it has been fixed. pillow.readthedocs.io/en/3.0.x/releasenotes/2.7.0.html
  • Mark Ransom
    Mark Ransom over 5 years
    @user666 that's good news! Thanks for the link to the release notes. They address every issue I found.