Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageDraw.line and Coords typing issue #8798

Open
Andrej730 opened this issue Mar 4, 2025 · 1 comment · May be fixed by #8800
Open

ImageDraw.line and Coords typing issue #8798

Andrej730 opened this issue Mar 4, 2025 · 1 comment · May be fixed by #8800

Comments

@Andrej730
Copy link

Andrej730 commented Mar 4, 2025

ImageDraw.line is currently using Coords for xy argument.

xy: Coords,

Coords = Union[Sequence[float], Sequence[Sequence[float]]]

Coords allows Sequence[Sequence], so it should support both tuple[tuple] and list[list] but currently it doesn't seem to be the case with ImageDraw.line - it fails with list[list] at runtime though there are no typing errors.

See example below.

from PIL import Image, ImageDraw
import numpy as np

img_size = (200, 200)
scale = 1.0

img = Image.new("RGB", img_size, "white")
draw = ImageDraw.Draw(img)

verts = np.array(
    [
        [[22.0, 105.0], [114.0, 105.0]],
        [[22.0, 105.0], [22.0, 23.0]],
        [[114.0, 105.0], [114.0, 113.0]],
        [[114.0, 113.0], [14.0, 113.0]],
        [[14.0, 113.0], [14.0, 15.0]],
        [[14.0, 15.0], [114.0, 15.0]],
        [[114.0, 15.0], [114.0, 23.0]],
        [[114.0, 23.0], [22.0, 23.0]],
    ]
)

for verts_ in verts:
    lst: list[list[float]]
    lst = verts_.tolist()
    # No typing errors but fails at runtime.
    # Traceback (most recent call last):
    # File "second_test.py", line 28, in <module>
    #     draw.line(verts_.tolist(), fill="black", width=2)
    # File "\Lib\site-packages\PIL\ImageDraw.py", line 249, in line
    #     self.draw.draw_lines(xy, ink, width)
    # ValueError: incorrect coordinate type
    # draw.line(lst, fill="black", width=2)

    # Works fine.
    tple = tuple(tuple(i) for i in verts_)
    draw.line(tple, fill="black", width=2)

# Show the image
img.show()
@radarhere radarhere linked a pull request Mar 4, 2025 that will close this issue
@radarhere
Copy link
Member

Hi. Prior to adding type hints, the idea of a sequence of lists for co-ordinates was discussed in #3738, and declined.

However, I suppose the fact that we have existing type hints that suggest this is a new argument. I've created #8800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants