Skip to content

ImageDraw.polygon is very slow when width > 1 #8976

@larsga

Description

@larsga

What did you do?

I'm generating maps from shapefiles. The full code is a bit too much to repeat here, and a reproducible sample is also tricky because of all the data needed. If desired I can produce a POC zip.

This is a cut-down version of the code:

img = Image.new('RGB', (self._view.width * RESIZE_FACTOR,
                        self._view.height * RESIZE_FACTOR),
                self._background.as_int_tuple(255))
draw = ImageDraw.Draw(img)

for layer in self._layers:
    # stuff to compute parameters
    draw.polygon(coords2, outline = lc, width = lw, fill = fc)

# resize to smooth image
if RESIZE_FACTOR != 1:
    img = img.resize((int(img.width / RESIZE_FACTOR),
                      int(img.height / RESIZE_FACTOR)),
                     resample = Image.Resampling.LANCZOS)

img.save(filename, 'PNG')

What did you expect to happen?

When lw above (line width) is 1 this runs in 4 seconds. When it's 3 the code takes 5 minutes. That seems ... unnecessary?

Of course, the ImageDraw.polygon method is being worked hard here, but if it can do the thin line in 4 seconds ...

What are your OS, Python and Pillow versions?

  • OS: MacOS 12.7.1
  • Python: 3.13.3
  • Pillow: 11.2.1 from pip
--------------------------------------------------------------------
Pillow 11.2.1
Python 3.13.3 (main, May 20 2025, 08:52:01) [Clang 14.0.0 (clang-1400.0.29.202)]
--------------------------------------------------------------------
Python executable is /usr/local/opt/python@3.13/bin/python3.13
System Python files loaded from /usr/local/opt/python@3.13/Frameworks/Python.framework/Versions/3.13
--------------------------------------------------------------------
Python Pillow modules loaded from /usr/local/lib/python3.13/site-packages/PIL
Binary Pillow modules loaded from /usr/local/lib/python3.13/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.2.1
*** TKINTER support not installed
--- FREETYPE2 support ok, loaded 2.13.3
--- LITTLECMS2 support ok, loaded 2.17
--- WEBP support ok, loaded 1.5.0
*** AVIF support not installed
--- JPEG support ok, compiled for libjpeg-turbo 3.1.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.3
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1
--- LIBTIFF support ok, loaded 4.7.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.16, harfbuzz 11.0.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions