Пример #1
0
 def select_text(self):
     if len(self._texts) == 1:
         # 经过init_text后,如果还是只有一个元素,则认为这个元素就是可显示在验证中所有字符的集合
         return ''.join([CaptchaUtils.random_choice_from(self._texts[0]) for i in range(self._num)])
     elif len(self._texts) > 1:
         # 否则,每个元素对应一张验证码的内容
         return CaptchaUtils.random_choice_from(self._texts).strip("\n")
Пример #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
Пример #3
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
Пример #4
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
Пример #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]
Пример #6
0
    def select_bg_custom_fn(self):
        fn = None
        if self._bg_custom_fns:
            fn = CaptchaUtils.random_choice_from(self._bg_custom_fns)

        return fn
Пример #7
0
 def select_background(self):
     bg = CaptchaUtils.random_choice_from(self._bgs)
     return bg
Пример #8
0
 def init_font_size(self):
     CaptchaUtils.random_choice_from(self._char_sizes)
Пример #9
0
 def draw_outline(self, actual_x, actual_y, w, h, color):
     CaptchaUtils.draw_rectangle(self._captcha, (actual_x, actual_y, w, h),
                                 color)
Пример #10
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
Пример #11
0
    def select_color(self):
        color = CaptchaUtils.random_choice_from(self._char_colors)

        return color
Пример #12
0
    def select_char_tran(self):
        char_tran = CaptchaUtils.random_choice_from(self._char_tran)

        return char_tran
Пример #13
0
    def select_size(self):
        size = CaptchaUtils.random_choice_from(self._char_sizes)

        return size
Пример #14
0
    def select_font(self):
        font = CaptchaUtils.random_choice_from(self._char_fonts)

        return font