From 345fb8397c62ed134cecbe7a9ea158ceb3a52a9e Mon Sep 17 00:00:00 2001 From: SecorD <25284138+SecorD0@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:16:15 +0300 Subject: [PATCH 1/4] Upgrade the 'generate_image' function --- captcha/image.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/captcha/image.py b/captcha/image.py index 4c5a4de..3ad5a6b 100644 --- a/captcha/image.py +++ b/captcha/image.py @@ -157,12 +157,13 @@ def create_noise_dots(image, color, width=3, number=30): number -= 1 return image - def create_captcha_image(self, chars, color, background): + def create_captcha_image(self, chars: str, color: tuple, background: tuple, rotate: bool = True): """Create the CAPTCHA image itself. :param chars: text to be generated. :param color: color of the text. :param background: color of the background. + :param rotate: whether to rotate the text? The color should be a tuple of 3 numbers, such as (0, 255, 255). """ @@ -182,8 +183,9 @@ def _draw_character(c): Draw(im).text((dx, dy), c, font=font, fill=color) # rotate - im = im.crop(im.getbbox()) - im = im.rotate(random.uniform(-30, 30), _BILINEAR, expand=1) + if rotate: + im = im.crop(im.getbbox()) + im = im.rotate(random.uniform(-30, 30), _BILINEAR, expand=1) # warp dx = w * random.uniform(0.1, 0.3) @@ -230,17 +232,23 @@ def _draw_character(c): return image - def generate_image(self, chars): + def generate_image(self, chars: str, rotate: bool = True, filters: list = []): """Generate the image of the given characters. :param chars: text to be generated. + :param rotate: whether to rotate the text? + :param filters: filter list from which a random one will be taken and applied to the image. """ background = random_color(238, 255) color = random_color(10, 200, random.randint(220, 255)) - im = self.create_captcha_image(chars, color, background) + im = self.create_captcha_image(chars, color, background, rotate) self.create_noise_dots(im, color) self.create_noise_curve(im, color) - im = im.filter(ImageFilter.SMOOTH) + filter = ImageFilter.SMOOTH + if filters: + filter = random.choice(filters) + + im = im.filter(filter) return im From 9b609cbcda8bbca63c90af7e21c7d9bd72a495a7 Mon Sep 17 00:00:00 2001 From: SecorD <25284138+SecorD0@users.noreply.github.com> Date: Fri, 14 Jul 2023 23:50:20 +0300 Subject: [PATCH 2/4] Disable rotating for special characters --- captcha/image.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/captcha/image.py b/captcha/image.py index 3ad5a6b..74db018 100644 --- a/captcha/image.py +++ b/captcha/image.py @@ -8,10 +8,13 @@ import os import random +import string + from PIL import Image from PIL import ImageFilter from PIL.ImageDraw import Draw from PIL.ImageFont import truetype + try: from cStringIO import StringIO as BytesIO except ImportError: @@ -170,7 +173,7 @@ def create_captcha_image(self, chars: str, color: tuple, background: tuple, rota image = Image.new('RGB', (self._width, self._height), background) draw = Draw(image) - def _draw_character(c): + def _draw_character(c: str, rotate: bool = True): font = random.choice(self.truefonts) try: _, _, w, h = draw.textbbox((1, 1), c, font=font) @@ -210,7 +213,12 @@ def _draw_character(c): for c in chars: if random.random() > 0.5: images.append(_draw_character(" ")) - images.append(_draw_character(c)) + + if c in string.punctuation: + images.append(_draw_character(c, False)) + + else: + images.append(_draw_character(c, rotate)) text_width = sum([im.size[0] for im in images]) From 68e5ca49f0a5d7ed5d30593bd77625d1955d479b Mon Sep 17 00:00:00 2001 From: SecorD <25284138+SecorD0@users.noreply.github.com> Date: Sat, 15 Jul 2023 15:29:29 +0300 Subject: [PATCH 3/4] Replace the list in the default value with None --- captcha/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/captcha/image.py b/captcha/image.py index 74db018..280fe49 100644 --- a/captcha/image.py +++ b/captcha/image.py @@ -240,7 +240,7 @@ def _draw_character(c: str, rotate: bool = True): return image - def generate_image(self, chars: str, rotate: bool = True, filters: list = []): + def generate_image(self, chars: str, rotate: bool = True, filters: list = None): """Generate the image of the given characters. :param chars: text to be generated. From f0ec72d4c8c3e4544701e58999c4ce3b013ac261 Mon Sep 17 00:00:00 2001 From: SecorD <25284138+SecorD0@users.noreply.github.com> Date: Mon, 17 Jul 2023 22:43:10 +0300 Subject: [PATCH 4/4] Delete type hints --- captcha/image.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/captcha/image.py b/captcha/image.py index 280fe49..f5c794b 100644 --- a/captcha/image.py +++ b/captcha/image.py @@ -160,7 +160,7 @@ def create_noise_dots(image, color, width=3, number=30): number -= 1 return image - def create_captcha_image(self, chars: str, color: tuple, background: tuple, rotate: bool = True): + def create_captcha_image(self, chars, color, background, rotate = True): """Create the CAPTCHA image itself. :param chars: text to be generated. @@ -173,7 +173,7 @@ def create_captcha_image(self, chars: str, color: tuple, background: tuple, rota image = Image.new('RGB', (self._width, self._height), background) draw = Draw(image) - def _draw_character(c: str, rotate: bool = True): + def _draw_character(c, rotate = True): font = random.choice(self.truefonts) try: _, _, w, h = draw.textbbox((1, 1), c, font=font) @@ -240,7 +240,7 @@ def _draw_character(c: str, rotate: bool = True): return image - def generate_image(self, chars: str, rotate: bool = True, filters: list = None): + def generate_image(self, chars, rotate = True, filters = None): """Generate the image of the given characters. :param chars: text to be generated.