What did you do?
I am trying to create a dataset containing Pentomino shapes for some experiment.
To define the shapes, I specify the vertices in the path. Since the shapes are composed of 5 squares in different configurations, distance between the corners must be uniform. In this case I have made each side be 2 units.
I plot them using Pillow, optinally performing some transformation of the input using Matplotlib's Affine2D functionality.
What did you expect to happen?
I expected the sides to be all of equal length.
What actually happened?
Pillow (or matplotlib) seems to add a couple of pixels to some sides and this creates a misalignment in the shape. This misalignment is magnified when performing transformations such as scaling (see example).
As an example, notice the right 'arm' in the 'R' shape used in the example or the dip in the 'U' (currently commented out in the example code).
I have tried manipulating the points ever so slightly but they never land in the right location. At first I thought I was going crazy because of how subtle it is, but for what I need to do, this slight variation is not ideal.
I have seen similar issues (367 and 3747), but they seemed to be solved...
What are your OS, Python and Pillow versions?
- OS: Ubuntu 20.10
- Python: 3.10.9
- Pillow: 9.5.0
"""
https://en.wikipedia.org/wiki/Pentomino
"""
import numpy as np
from PIL import Image, ImageDraw
from matplotlib.path import Path
from matplotlib.transforms import Affine2D
height, width = 60, 60
# R
coordinates = np.array([
[4, 8, 8, 6, 6, 4, 4, 2, 2, 4],
[2, 2, 4, 4, 8, 8, 6, 6, 4, 4],
]).T
# U
# coordinates = np.array([
# [2, 4, 4, 6, 6, 8, 8, 2],
# [3, 3, 5, 5, 3, 3, 7, 7],
# ]).T
coordinates = coordinates * (width // 10, height // 10)
path = Path(coordinates)
# transform = (
# Affine2D().translate(-0.5 * width, -0.5 * height) +
# Affine2D().scale(0.5) +
# Affine2D().rotate_deg(90.) +
# Affine2D().translate(0.5 * width, 0.5 * height)
# )
# path = transform.transform_path(path)
canvas = Image.new('RGB', (height, width), (0, 0, 0))
draw = ImageDraw.Draw(canvas)
color = (255, 0, 0)
draw.polygon([tuple(v) for v in path.vertices], fill=color)
image = Image.new('RGB', (height + 4, width + 4), color=(0, 0, 0))
image.paste(canvas, (2, 2))
image.save('example.png')
What did you do?
I am trying to create a dataset containing Pentomino shapes for some experiment.
To define the shapes, I specify the vertices in the path. Since the shapes are composed of 5 squares in different configurations, distance between the corners must be uniform. In this case I have made each side be 2 units.
I plot them using Pillow, optinally performing some transformation of the input using Matplotlib's Affine2D functionality.
What did you expect to happen?
I expected the sides to be all of equal length.
What actually happened?
Pillow (or matplotlib) seems to add a couple of pixels to some sides and this creates a misalignment in the shape. This misalignment is magnified when performing transformations such as scaling (see example).
As an example, notice the right 'arm' in the 'R' shape used in the example or the dip in the 'U' (currently commented out in the example code).
I have tried manipulating the points ever so slightly but they never land in the right location. At first I thought I was going crazy because of how subtle it is, but for what I need to do, this slight variation is not ideal.
I have seen similar issues (367 and 3747), but they seemed to be solved...
What are your OS, Python and Pillow versions?