Skip to content

Commit

Permalink
fix: Deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Леонтьев Арсений Евгеньевич committed Sep 17, 2022
1 parent a666f57 commit e9047d5
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions captcha/image.py
Expand Up @@ -29,6 +29,15 @@
else:
__all__ = ['ImageCaptcha']

try:
_QUAD = Image.Transform.QUAD
except AttributeError:
_QUAD = Image.QUAD

try:
_BILINEAR = Image.Resampling.BILINEAR
except AttributeError:
_BILINEAR = Image.BILINEAR

table = []
for i in range( 256 ):
Expand Down Expand Up @@ -162,16 +171,18 @@ def create_captcha_image(self, chars, color, background):

def _draw_character(c):
font = random.choice(self.truefonts)
w, h = draw.textsize(c, font=font)

try:
_, _, w, h = draw.multiline_textbbox((1, 1),c, font=font)
except AttributeError:
w, h = draw.textsize(c, font=font)
dx = random.randint(0, 4)
dy = random.randint(0, 6)
im = Image.new('RGBA', (w + dx, h + dy))
Draw(im).text((dx, dy), c, font=font, fill=color)

# rotate
im = im.crop(im.getbbox())
im = im.rotate(random.uniform(-30, 30), Image.BILINEAR, expand=1)
im = im.rotate(random.uniform(-30, 30), _BILINEAR, expand=1)

# warp
dx = w * random.uniform(0.1, 0.3)
Expand All @@ -189,7 +200,7 @@ def _draw_character(c):
w2 - x2, -y1,
)
im = im.resize((w2, h2))
im = im.transform((w, h), Image.QUAD, data)
im = im.transform((w, h), _QUAD, data)
return im

images = []
Expand Down

0 comments on commit e9047d5

Please sign in to comment.