Exemple #1
0
def bg_custom_fn(bg):
    # do something you wanted
    # return bg.filter(ImageFilter.GaussianBlur)
    CaptchaUtils.draw_line(
        bg, (0, CaptchaUtils.random_choice_from(range(12, 14))),
        (85, CaptchaUtils.random_choice_from(range(13, 15))),
        CaptchaUtils.get_rgb("0x595e12"), 1)
    CaptchaUtils.draw_line(
        bg, (0, CaptchaUtils.random_choice_from(range(11, 16))),
        (85, CaptchaUtils.random_choice_from(range(17, 21))),
        CaptchaUtils.get_rgb("0x563c7c"), 1)
    return bg
Exemple #2
0
    def joint_image(self):
        average = int(self._width / self._num)
        except_x = 0

        for char in self._chars:
            _char = char.char
            # _char.show()
            w, h = _char.size
            except_y = int((self._height - h) / 2)

            if char.position_x is None:
                char.set_x(self.get_actual_x(except_x, w))
            if char.position_y is None:
                char.set_y(self.get_actual_y(except_y, h))

            self._captcha.paste(_char, (char.position_x, char.position_y),
                                char.mask)
            # self._captcha.show()
            self._char_pos.append((char.position_x, char.position_y, w, h))

            if self._outline_color:
                color = CaptchaUtils.get_rgb(self._outline_color)
                self.draw_outline(char.position_x, char.position_y, w, h,
                                  color)
            # 左对齐
            if self._align == 1:
                except_x += _char.width
            else:  # 两端对齐
                except_x += average
Exemple #3
0
    def init_captcha(self):
        tmp_captcha = None
        if os.path.isfile(self._background):
            tmp_captcha = Image.open(self._background).resize(
                (self._width, self._height))
        else:
            bg = CaptchaUtils.get_rgb(self._background)
            tmp_captcha = Image.new('RGB', (self._width, self._height), bg)

        if self._bg_custom_fn:
            tmp_captcha = self._bg_custom_fn(tmp_captcha)

        return tmp_captcha
Exemple #4
0
 def draw_noise_curve(self,
                      color="0x484848",
                      number=4,
                      width=3,
                      type="line"):
     points = [
         CaptchaUtils.random_point(self._width, self._height)
         for i in range(number + 1)
     ]
     if type == "line":
         for idx in range(len(points) - 1):
             CaptchaUtils.draw_line(self._captcha, points[idx],
                                    points[idx + 1],
                                    CaptchaUtils.get_rgb(color), width)
     elif type == "curve":
         pass
     # CaptchaUtils.draw_curve()
     else:
         pass
Exemple #5
0
    def create_character(self):
        font = self.generate_true_font()
        w, h = self._draw.textsize(self._char_text, font=font)

        self._char_image = Image.new('RGB', (w, h), 0)  # (255, 255, 255)
        color = CaptchaUtils.get_rgb(self._color)
        Draw(self._char_image).text((0, 0),
                                    self._char_text,
                                    font=font,
                                    fill=color)
        # self._char_image.show()
        self._char_image = self._char_image.rotate(self._rotate,
                                                   Image.BILINEAR,
                                                   expand=1)
        self._char_image = self._char_image.crop(self._char_image.getbbox())
        if self._custom_fn:
            tmp = self._char_image
            self._char_image = self._custom_fn(tmp)
            self._char_image = self._char_image.crop(
                self._char_image.getbbox())

        self._width = self._char_image.size[0]
        self._height = self._char_image.size[1]
Exemple #6
0
 def draw_noise_dot(self, color="0x484848", width=3, number=30):
     while number:
         CaptchaUtils.draw_dot(self._captcha,
                               color=CaptchaUtils.get_rgb(color),
                               width=width)
         number -= 1