Merge pull request #2523 from joachimesque/update-pillow-deprecated

Remove deprecation warnings from Pillow
This commit is contained in:
Mouse Reeve 2022-12-19 12:27:13 -08:00 committed by GitHub
commit 271337a7e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 20 deletions

View file

@ -71,20 +71,29 @@ def get_wrapped_text(text, font, content_width):
low = 0
high = len(text)
draw = ImageDraw.Draw(Image.new("RGB", (100, 100)))
try:
# ideal length is determined via binary search
while low < high:
mid = math.floor(low + high)
wrapped_text = textwrap.fill(text, width=mid)
width = font.getsize_multiline(wrapped_text)[0]
left, top, right, bottom = draw.multiline_textbbox(
(0, 0), wrapped_text, font=font
)
width = right - left
height = bottom - top
if width < content_width:
low = mid
else:
high = mid - 1
except AttributeError:
wrapped_text = text
height = 26
return wrapped_text
return wrapped_text, height
def generate_texts_layer(texts, content_width):
@ -100,47 +109,53 @@ def generate_texts_layer(texts, content_width):
text_y = 0
if "text_zero" in texts and texts["text_zero"]:
# Text one (Book title)
text_zero = get_wrapped_text(texts["text_zero"], font_text_zero, content_width)
# Text zero (Site preview domain name)
text_zero, text_height = get_wrapped_text(
texts["text_zero"], font_text_zero, content_width
)
text_layer_draw.multiline_text(
(0, text_y), text_zero, font=font_text_zero, fill=TEXT_COLOR
)
try:
text_y = text_y + font_text_zero.getsize_multiline(text_zero)[1] + 16
text_y = text_y + text_height + 16
except (AttributeError, IndexError):
text_y = text_y + 26
if "text_one" in texts and texts["text_one"]:
# Text one (Book title)
text_one = get_wrapped_text(texts["text_one"], font_text_one, content_width)
# Text one (Book/Site title, User display name)
text_one, text_height = get_wrapped_text(
texts["text_one"], font_text_one, content_width
)
text_layer_draw.multiline_text(
(0, text_y), text_one, font=font_text_one, fill=TEXT_COLOR
)
try:
text_y = text_y + font_text_one.getsize_multiline(text_one)[1] + 16
text_y = text_y + text_height + 16
except (AttributeError, IndexError):
text_y = text_y + 26
if "text_two" in texts and texts["text_two"]:
# Text one (Book subtitle)
text_two = get_wrapped_text(texts["text_two"], font_text_two, content_width)
# Text two (Book subtitle)
text_two, text_height = get_wrapped_text(
texts["text_two"], font_text_two, content_width
)
text_layer_draw.multiline_text(
(0, text_y), text_two, font=font_text_two, fill=TEXT_COLOR
)
try:
text_y = text_y + font_text_one.getsize_multiline(text_two)[1] + 16
text_y = text_y + text_height + 16
except (AttributeError, IndexError):
text_y = text_y + 26
if "text_three" in texts and texts["text_three"]:
# Text three (Book authors)
text_three = get_wrapped_text(
# Text three (Book authors, Site tagline, User address)
text_three, _ = get_wrapped_text(
texts["text_three"], font_text_three, content_width
)
@ -172,7 +187,7 @@ def generate_instance_layer(content_width):
instance_text_x = 0
if logo_img:
logo_img.thumbnail((50, 50), Image.ANTIALIAS)
logo_img.thumbnail((50, 50), Image.Resampling.LANCZOS)
instance_layer.paste(logo_img, (0, 0))
@ -183,7 +198,7 @@ def generate_instance_layer(content_width):
(instance_text_x, 10), site.name, font=font_instance, fill=TEXT_COLOR
)
line_width = 50 + 10 + font_instance.getsize(site.name)[0]
line_width = 50 + 10 + round(font_instance.getlength(site.name))
line_layer = Image.new(
"RGBA", (line_width, 2), color=(*(ImageColor.getrgb(TEXT_COLOR)), 50)
@ -253,10 +268,12 @@ def generate_default_inner_img():
default_cover_draw = ImageDraw.Draw(default_cover)
text = "no image :("
text_dimensions = font_cover.getsize(text)
text_left, text_top, text_right, text_bottom = font_cover.getbbox(text)
text_width, text_height = text_right - text_left, text_bottom - text_top
text_coords = (
math.floor((inner_img_width - text_dimensions[0]) / 2),
math.floor((inner_img_height - text_dimensions[1]) / 2),
math.floor((inner_img_width - text_width) / 2),
math.floor((inner_img_height - text_height) / 2),
)
default_cover_draw.text(text_coords, text, font=font_cover, fill="white")
@ -273,7 +290,9 @@ def generate_preview_image(
# Cover
try:
inner_img_layer = Image.open(picture)
inner_img_layer.thumbnail((inner_img_width, inner_img_height), Image.ANTIALIAS)
inner_img_layer.thumbnail(
(inner_img_width, inner_img_height), Image.Resampling.LANCZOS
)
color_thief = ColorThief(picture)
dominant_color = color_thief.get_color(quality=1)
except: # pylint: disable=bare-except

View file

@ -12,7 +12,7 @@ environs==9.3.4
flower==1.2.0
libsass==0.21.0
Markdown==3.3.3
Pillow>=9.0.0
Pillow>=9.3.0
psycopg2==2.8.4
pycryptodome==3.9.4
python-dateutil==2.8.1