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

Pillow10 #9

Merged
merged 3 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_version():
install_requires=[
'setuptools',
'funcparserlib>=1.0.0a0',
'Pillow > 3.0, < 10.0',
'Pillow > 3.0',
'webcolors',
],
extras_require={
Expand Down
46 changes: 38 additions & 8 deletions src/blockdiag/imagedraw/png.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@
from blockdiag.utils.fontmap import FontMap, parse_fontpath
from blockdiag.utils.myitertools import istep, stepslice

# to support pillow < 9.1.0
if not hasattr(Image, 'Resampling'):
from enum import IntEnum

class Resampling(IntEnum):
NEAREST = 0
BOX = 4
BILINEAR = 2
HAMMING = 5
BICUBIC = 3
LANCZOS = 1

Image.Resampling = Resampling


def point_pairs(xylist):
iterable = iter(xylist)
Expand Down Expand Up @@ -147,7 +161,7 @@ def set_canvas_size(self, size):
self.draw = ImageDraw.Draw(self._image)

def resizeCanvas(self, size):
self._image = self._image.resize(size, Image.ANTIALIAS)
self._image = self._image.resize(size, Image.Resampling.LANCZOS)
self.draw = ImageDraw.Draw(self._image)

def arc(self, box, start, end, **kwargs):
Expand Down Expand Up @@ -273,13 +287,21 @@ def textfolder(self):
def textlinesize(self, string, font):
ttfont = ttfont_for(font)
if ttfont is None:
size = self.draw.textsize(string, font=None)
if hasattr(self.draw, 'textbbox'):
left, top, right, bottom = self.draw.textbbox((0, 0), string)
size = (right - left, bottom - top)
else:
size = self.draw.textsize(string, font=None)

font_ratio = font.size * 1.0 / FontMap.BASE_FONTSIZE
size = Size(int(size[0] * font_ratio),
int(size[1] * font_ratio))
else:
size = Size(*ttfont.getsize(string))
if hasattr(ttfont, 'getbbox'):
left, top, right, bottom = ttfont.getbbox(string)
size = Size(right - left, bottom - top)
else:
size = Size(*ttfont.getsize(string))

return size

Expand All @@ -291,18 +313,26 @@ def text(self, xy, string, font, **kwargs):
if self.scale_ratio == 1 and font.size == FontMap.BASE_FONTSIZE:
self.draw.text(xy, string, fill=fill)
else:
size = self.draw.textsize(string)
if hasattr(self.draw, 'textbbox'):
left, top, right, bottom = self.draw.textbbox((0, 0), string)
size = (right - left, bottom - top)
else:
size = self.draw.textsize(string)
image = Image.new('RGBA', size)
draw = ImageDraw.Draw(image)
draw.text((0, 0), string, fill=fill)
del draw

basesize = (size[0] * self.scale_ratio,
size[1] * self.scale_ratio)
text_image = image.resize(basesize, Image.ANTIALIAS)
text_image = image.resize(basesize, Image.Resampling.LANCZOS)
self.paste(text_image, xy, text_image)
else:
size = ttfont.getsize(string)
if hasattr(ttfont, 'getbbox'):
left, top, right, bottom = ttfont.getbbox(string)
size = (right - left, bottom - top)
else:
size = ttfont.getsize(string)

# Generate mask to support BDF(bitmap font)
mask = Image.new('1', size)
Expand Down Expand Up @@ -370,7 +400,7 @@ def image(self, box, url):
# resize image.
w = min([box.width, image.size[0] * self.scale_ratio])
h = min([box.height, image.size[1] * self.scale_ratio])
image.thumbnail((w, h), Image.ANTIALIAS)
image.thumbnail((w, h), Image.Resampling.LANCZOS)

# centering image.
w, h = image.size
Expand Down Expand Up @@ -404,7 +434,7 @@ def save(self, filename, size, _format):
y = int(self._image.size[1] / self.scale_ratio)
size = (x, y)

self._image.thumbnail(size, Image.ANTIALIAS)
self._image.thumbnail(size, Image.Resampling.LANCZOS)

if self.filename:
self._image.save(self.filename, _format)
Expand Down
Loading