27

For lines and ellipses in PIL, the images are rough.

I found antialiasing only in resize and thumbnail.

Is there any way to do antialiasing when drawing a line or ellipse?

1
  • 1
    You could do a low-pass filtering :) You will probably want to move to aggdraw (or cairo, or ...) for "fancier" drawing.
    – mmgp
    Jan 16, 2013 at 3:02

2 Answers 2

42

The only way to do it natively is with supersampling. Render your image at a multiple of the size you require, then resize it with resample=Image.LANCZOS, e.g.:

im = im.resize((width // 2, height // 2), resample=Image.LANCZOS)

If you have a very old version of PIL (before Pillow 2.7.0) you need to use ANTIALIAS instead of LANCZOS. The ANTIALIAS constant which was originally recommended with this answer has been deprecated for years, and was finally removed in Pillow 10.0.

6
  • 2
    @Hugo: In the Image.resize() method of the current stable version of the pillow fork of PIL, the keyword argument is named resample. This may have been changed in version 2.7.0. Also note that Image.LANCZOS is the same thing as Image.ANTIALIAS—which isn't mentioned in the method's documentation.
    – martineau
    Apr 29, 2019 at 19:14
  • 2
    img.resize((width, height), Image.ANTIALIAS) works for me. Thank you !
    – cdrom
    Feb 10, 2020 at 15:44
  • I was having this issue with an ellipse mask and following answer helped me: stackoverflow.com/a/22336005/1092815 Same answer, but with complete code example
    – GabLeRoux
    Mar 30, 2020 at 14:06
  • 1
    This is great. Note that 3 times multiplier gives much better results than 2 times, and I felt actually better than 4 times. Nov 14, 2021 at 10:30
  • 1
    PIL.Image.ANTIALIAS nowadays (Pillow 9.2.0)
    – Conor
    Sep 12, 2022 at 15:20
5

aggdraw (http://effbot.org/zone/aggdraw-index.htm) may be something you're interested in.

The aggdraw module implements the basic WCK 2D Drawing Interface on top of the AGG library. This library provides high-quality drawing, with anti-aliasing and alpha compositing, while being fully compatible with the WCK renderer.

The aggdraw module can be used with PIL or the WCK library (under Tkinter or native Windows). It can also be used as a stand-alone library.

2
  • Is there a port for OS X? Mar 29, 2016 at 14:19
  • 1
    Aggdraw is available on Linux, OSX, and Windows. It can be installed from PyPI with pip.
    – allcaps
    Jun 21, 2022 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.