Skip to content

Commit

Permalink
Merge pull request #1 from nicdumz/main
Browse files Browse the repository at this point in the history
Fix deprecation of Pillow ImageDraw textsize()
  • Loading branch information
jamesridgway authored Jun 1, 2024
2 parents fd55653 + caeb3c7 commit 7899170
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions devdeck_core/deck_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def render_text(self, key_no, text, **kwargs):

image = Image.new("RGB", (512, 512))
draw = ImageDraw.Draw(image)
label_w, label_h = draw.textsize('%s' % text, font=font)
label_pos = ((512 - label_w) // 2, (512 - label_h) // 2)
left, top, right, bottom = draw.textbbox((0, 0), '%s' % text, font=font)
label_pos = ((512 - (right - left)) // 2, (512 - (bottom - top)) // 2)
draw.text(label_pos, text=text, font=font, fill=fill)
self.set_key_image_native(key_no, image)

def get_image(self):
return self.image
return self.image
6 changes: 3 additions & 3 deletions devdeck_core/rendering/badge_count_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def end(self):
font = ImageFont.truetype(self.font_filename, self._font_size)

draw = ImageDraw.Draw(self.renderer.img)
label_w, label_h = draw.textsize('%s' % self.text, font=font, stroke_width=4)
left, top, right, bottom = draw.textbbox((0, 0), '%s' % self.text, font=font, stroke_width=4)

# Circle

Expand All @@ -31,8 +31,8 @@ def end(self):
self.renderer.img.height - self.corner_offset), fill='red')

# Label
label_pos = (self.renderer.img.width - (self.circle_size / 2) - self.corner_offset - (label_w / 2),
self.renderer.img.height - (self.circle_size / 2) - self.corner_offset - (label_h / 2))
label_pos = (self.renderer.img.width - (self.circle_size / 2) - self.corner_offset - ((right - left) / 2),
self.renderer.img.height - (self.circle_size / 2) - self.corner_offset - ((bottom - top) / 2))
draw.text(label_pos, text=self.text, font=font, fill=self.fill, stroke_width=4)

return self.renderer
6 changes: 3 additions & 3 deletions devdeck_core/rendering/text_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def end(self):
font = ImageFont.truetype(self.font_filename, self._font_size)

draw = ImageDraw.Draw(self.renderer.img)
label_w, label_h = draw.textsize('%s' % self.text, font=font)
left, top, right, bottom = draw.textbbox((0, 0), '%s' % self.text, font=font)

# Positioning
label_pos = (self._x, self._y)
if self.center_vertical is not None:
label_pos = (label_pos[0], ((self.renderer.img.height - label_h) // 2) + self.center_vertical)
label_pos = (label_pos[0], ((self.renderer.img.height - (bottom - top)) // 2) + self.center_vertical)
if self.center_horizontal is not None:
label_pos = (((self.renderer.img.width - label_w) // 2) + self.center_horizontal, label_pos[1])
label_pos = (((self.renderer.img.width - (right - left)) // 2) + self.center_horizontal, label_pos[1])

draw.text(label_pos, text=self.text, font=font, fill=self.fill, align=self.align)

Expand Down

0 comments on commit 7899170

Please sign in to comment.