From e9047d5d2ce1def459db111ccc1e8ea38bb9425b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D1=82=D1=8C=D0=B5=D0=B2=20=D0=90?= =?UTF-8?q?=D1=80=D1=81=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=95=D0=B2=D0=B3=D0=B5?= =?UTF-8?q?=D0=BD=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Sat, 17 Sep 2022 17:31:02 +0400 Subject: [PATCH] fix: Deprecation warning --- captcha/image.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/captcha/image.py b/captcha/image.py index 8816566..fd2620c 100644 --- a/captcha/image.py +++ b/captcha/image.py @@ -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 ): @@ -162,8 +171,10 @@ 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)) @@ -171,7 +182,7 @@ def _draw_character(c): # 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) @@ -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 = []