python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.27k stars 2.23k forks source link

Misplaced polygon outline #8111

Closed anloubie2 closed 4 months ago

anloubie2 commented 5 months ago

What did you do?

I drew a polygon on an image (16 bits grayscale)

What did you expect to happen?

The polygon should have been created at the specified coordinates

What actually happened?

The polygon was

What are your OS, Python and Pillow versions?

Please paste here the output of running:
python3 -m PIL --report

---------------------------------------------------------------------
Pillow 10.3.0
Python 3.10.11 (main, Apr  7 2023, 07:24:53) [Clang 14.0.0 (clang-1400.0.29.202)]
--------------------------------------------------------------------
Python executable is /Users/antoine/Envs/jupyter/bin/python3
Environment Python files loaded from /Users/antoine/Envs/jupyter
System Python files loaded from /opt/homebrew/opt/python@3.10/Frameworks/Python.framework/Versions/3.10
--------------------------------------------------------------------
Python Pillow modules loaded from /Users/antoine/Envs/jupyter/lib/python3.10/site-packages/PIL
Binary Pillow modules loaded from /Users/antoine/Envs/jupyter/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.3.0
*** TKINTER support not installed
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 3.0.2
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1
--- LIBTIFF support ok, loaded 4.6.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.13, harfbuzz 8.4.0
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------

When trying to draw a polygon on a 16 bits grayscale image, everything works fine with version 10.2.0 but the polygon is misplaced and its outline color is ignored with version 10.3.0.

Note that both these issues are fixed in 10.3.0 if a fill color is specified as well

10.2.0 with only outline 10.3.0 with only outline 10.3.0 with both outline and fill
10_2_0_outline 10_3_0_outline 10_3_0_fill_outline
from PIL import Image, ImageDraw

IMAGE_PATH = "./image.png"
WHITE = 65536
COORDINATES = [(1284.0, 1852.0), (1485.0, 1940.0), (1422.0, 2053.0), (1233.0, 1965.0)]

if __name__ == '__main__':
    image = Image.open(IMAGE_PATH)
    draw = ImageDraw.Draw(image)
    # This example works in 10.2.0 but not in 10.3.0
    draw.polygon(COORDINATES, outline=WHITE, width=15)
    # This example works in both 10.2.0 and 10.3.0
    # draw.polygon(COORDINATES, fill=WHITE, outline=WHITE, width=15)
    image.save('results.png')
    image.close()

Source image here: image

radarhere commented 5 months ago

The PNG you've attached is actually in L mode, not I;16*, so I can't replicate this exactly, but I expect #7849 is what triggered this change, by changing 16-bit grayscale PNGs to open in I;16 mode rather than I mode.

I've created #8112 to fix the polygon position. Your value for WHITE is slightly too high for a 16-bit image at 65536 (0x10000), but if you change it to 65535 (0xFFFF), it should work.