Skip to content

Commit

Permalink
compat for PIL = 10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Aug 2, 2023
1 parent debe5a0 commit 4572ee2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
9 changes: 7 additions & 2 deletions pyllusion/image/image_circle.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import numpy as np
import PIL.Image, PIL.ImageDraw, PIL.ImageFilter, PIL.ImageFont, PIL.ImageOps
import PIL.Image
import PIL.ImageDraw
import PIL.ImageFilter
import PIL.ImageFont
import PIL.ImageOps

from .utilities import _color, _coord_circle


def image_circles(
width=500,
height=500,
Expand Down Expand Up @@ -202,7 +207,7 @@ def image_circle(

# resize with antialiasing
if antialias is True:
mask = mask.resize(image.size, PIL.Image.ANTIALIAS)
mask = mask.resize(image.size, PIL.Image.Resampling.LANCZOS)

# Blur the image a bit
if blur > 0:
Expand Down
8 changes: 6 additions & 2 deletions pyllusion/image/image_line.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import PIL.Image, PIL.ImageDraw, PIL.ImageFilter, PIL.ImageFont, PIL.ImageOps
import PIL.Image
import PIL.ImageDraw
import PIL.ImageFilter
import PIL.ImageFont
import PIL.ImageOps

from .utilities import _coord_line

Expand Down Expand Up @@ -118,7 +122,7 @@ def image_line(

# resize with antialiasing
if antialias is True:
mask = mask.resize(image.size, PIL.Image.ANTIALIAS)
mask = mask.resize(image.size, PIL.Image.Resampling.LANCZOS)

# Blur the image a bit
if blur > 0:
Expand Down
10 changes: 7 additions & 3 deletions pyllusion/image/image_rectangle.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import PIL.Image, PIL.ImageDraw, PIL.ImageFilter, PIL.ImageFont, PIL.ImageOps
import PIL.Image
import PIL.ImageDraw
import PIL.ImageFilter
import PIL.ImageFont
import PIL.ImageOps

from .utilities import _color, _coord_rectangle

Expand Down Expand Up @@ -63,7 +67,7 @@ def image_rectangle(
If set to True, the size_height can be adjusted to the height and width of the
image.
image : Image
If None, an image will be created.
If None, an image will be created.
Returns
-------
Expand Down Expand Up @@ -120,7 +124,7 @@ def image_rectangle(

# resize with antialiasing
if antialias is True:
mask = mask.resize(image.size, PIL.Image.ANTIALIAS)
mask = mask.resize(image.size, PIL.Image.Resampling.LANCZOS)

# Blur the image a bit
if blur > 0:
Expand Down
27 changes: 16 additions & 11 deletions pyllusion/image/utilities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import PIL.ImageColor, PIL.ImageFont
import PIL.ImageColor
import PIL.ImageFont

from .rescale import rescale

Expand Down Expand Up @@ -59,7 +60,7 @@ def _coord_circle(image, diameter=0.1, x=0, y=0, unit="grid", method="pil"):
# Convert diameter based on height
diameter = int(rescale(diameter, to=[0, height], scale=[0, 2]))
diameter = 2 if diameter < 2 else diameter

radius = diameter / 2
# Choose diameter and centre
coord = [(x - radius, y - radius), (x + radius, y + radius)]
Expand All @@ -84,7 +85,7 @@ def _coord_text(
>>> image = PIL.Image.new('RGB', (500, 500), color = "white")
>>> draw = PIL.ImageDraw.Draw(image, 'RGB')
>>>
>>> coord, font = _coord_text(image, size="auto", x=-0.5, y=0.5) #doctest: +SKIP
>>> coord, font, x , y = _coord_text(image, size="auto", x=-0.5, y=0.5) #doctest: +SKIP
>>> draw.text(coord, text="hello", fill="black", font=font) #doctest: +SKIP
>>> image #doctest: +SKIP
"""
Expand All @@ -107,16 +108,20 @@ def _coord_text(
and top_left_y > 0.01 * height
and bottom_y < 0.99 * height
):
loaded_font = PIL.ImageFont.truetype(font, size)
text_width, text_height = loaded_font.getsize(text)
top_left_x = x - (text_width / 2)
top_left_y = y - (text_height / 2)
right_x = top_left_x + text_width
bottom_y = top_left_y + text_height
size += 1 # Increment text size
try: # In case size is too small
loaded_font = PIL.ImageFont.truetype(font, size)
text_width, text_height = loaded_font.getbbox(text)[2:4]
top_left_x = x - (text_width / 2)
top_left_y = y - (text_height / 2)
right_x = top_left_x + text_width
bottom_y = top_left_y + text_height
except OSError:
pass
size += 0.5 # Increment text size
else:
loaded_font = PIL.ImageFont.truetype(font, size)
text_width, text_height = loaded_font.getsize(text)
# text_width, text_height = loaded_font.getsize(text)
text_width, text_height = loaded_font.getbbox(text)[2:4]
top_left_x = x - (text_width / 2)
top_left_y = y - (text_height / 2)

Expand Down

0 comments on commit 4572ee2

Please sign in to comment.