Esempio n. 1
0
    def text(self,
             fonts,
             font_sizes=None,
             drawings=None,
             color='#FFFFFF',
             squeeze_factor=0.5,
             border=2,
             b_color="#000000"):
        fonts = tuple([
            truetype(name, size) for name in fonts
            for size in font_sizes or (65, 70, 75)
        ])
        if not callable(color):
            c = getrgb(color)

            def color():
                return c

        if not callable(b_color):
            bc = getrgb(b_color)

            def b_color():
                return bc

        def drawer(image, text):
            draw = Draw(image)
            char_images = []
            for c in text:
                font = random.choice(fonts)
                c_width, c_height = draw.textsize(c, font=font)
                char_image = Image.new('RGBA', (c_width + 1, c_height + 1),
                                       "black")
                char_draw = Draw(char_image)
                self._outline(char_draw, 0, 0, c, font, "gray", border)
                char_draw.text((0, 0), c, font=font, fill="black")

                char_image = char_image.crop(char_image.getbbox())
                for drawing in drawings:
                    char_image = drawing(char_image)
                char_image = char_image.resize((char_image.width, 42))
                char_images.append(char_image)
            width, height = image.size
            offset = int((width - sum(
                int(i.size[0] * squeeze_factor)
                for i in char_images[:-1]) - char_images[-1].size[0]) / 2)
            for char_image in char_images:
                c_width, c_height = char_image.size
                mask = char_image.convert('L').point(lambda i: i * 1.97)
                image.paste(char_image, (offset, int((height - c_height) / 2)),
                            mask)
                offset += int(c_width * squeeze_factor)
            self._remove_white_background_with_chops(image)
            return image

        return drawer
Esempio n. 2
0
def background(color='#EEEECC'):
    color = getrgb(color)

    def drawer(image, text):
        Draw(image).rectangle([(0, 0), image.size], fill=color)
        return image
    return drawer
Esempio n. 3
0
def background(color='#EEEECC'):
    color = getrgb(color)

    def drawer(image, text):
        Draw(image).rectangle([(0, 0), image.size], fill=color)
        return image
    return drawer
Esempio n. 4
0
def text(fonts, font_sizes=None, drawings=None, color='#5C87B2',
         squeeze_factor=0.8):
    fonts = tuple([truetype(name, size)
                   for name in fonts
                   for size in font_sizes or (65, 70, 75)])
    if not callable(color):
        c = getrgb(color)

        def color():
            return c

    def drawer(image, text):
        draw = Draw(image)
        char_images = []
        for c in text:
            font = random.choice(fonts)
            c_width, c_height = draw.textsize(c, font=font)
            #char_image = Image.new('RGB', (c_width, c_height), (0, 0, 0))
            o_width, o_height = font.getoffset(c)
            char_image = Image.new(
                'RGB',
                (c_width + o_width, c_height + o_height),
                (0, 0, 0)
            )
            char_draw = Draw(char_image)
            char_draw.text((0, 0), c, font=font, fill=color())
            char_image = char_image.crop(char_image.getbbox())
            for drawing in drawings:
                char_image = drawing(char_image)
            char_images.append(char_image)
        width, height = image.size
        offset = int((width - sum(int(i.size[0] * squeeze_factor)
                                  for i in char_images[:-1]) -
                      char_images[-1].size[0]) / 2)
        for char_image in char_images:
            c_width, c_height = char_image.size
            mask = char_image.convert('L').point(lambda i: i * 1.97)
            image.paste(char_image,
                        (offset, int((height - c_height) / 2)),
                        mask)
            offset += int(c_width * squeeze_factor)
        return image
    return drawer
Esempio n. 5
0
def noise(number=50, color='#EEEECC', level=2):
    if not callable(color):
        c = getrgb(color)

        def color():
            return c

    def drawer(image, text):
        width, height = image.size
        dx = width / 10
        width = width - dx
        dy = height / 10
        height = height - dy
        draw = Draw(image)
        for i in xrange(number):
            x = int(random.uniform(dx, width))
            y = int(random.uniform(dy, height))
            draw.line(((x, y), (x + level, y)), fill=color(), width=level)
        return image
    return drawer
Esempio n. 6
0
def noise(number=50, color='#EEEECC', level=2):
    if not callable(color):
        c = getrgb(color)

        def color():
            return c

    def drawer(image, text):
        width, height = image.size
        dx = width / 10
        width = width - dx
        dy = height / 10
        height = height - dy
        draw = Draw(image)
        for i in xrange(number):
            x = int(random.uniform(dx, width))
            y = int(random.uniform(dy, height))
            draw.line(((x, y), (x + level, y)), fill=color(), width=level)
        return image
    return drawer
Esempio n. 7
0
def curve(color='#5C87B2', width=4, number=6):
    from wheezy.captcha.bezier import make_bezier
    if not callable(color):
        c = getrgb(color)

        def color():
            return c

    def drawer(image, text):
        dx, height = image.size
        dx = dx / number
        path = [(dx * i, random.randint(0, height))
                for i in range(1, number)]
        bcoefs = make_bezier(number - 1)
        points = []
        for coefs in bcoefs:
            points.append(tuple(sum([coef * p for coef, p in zip(coefs, ps)])
                          for ps in zip(*path)))
        draw = Draw(image)
        draw.line(points, fill=color(), width=width)
        return image
    return drawer
Esempio n. 8
0
def curve(color='#5C87B2', width=4, number=6):
    from wheezy.captcha.bezier import make_bezier
    if not callable(color):
        c = getrgb(color)

        def color():
            return c

    def drawer(image, text):
        dx, height = image.size
        dx = dx / number
        path = [(dx * i, random.randint(0, height))
                for i in range(1, number)]
        bcoefs = make_bezier(number - 1)
        points = []
        for coefs in bcoefs:
            points.append(tuple(sum([coef * p for coef, p in zip(coefs, ps)])
                          for ps in zip(*path)))
        draw = Draw(image)
        draw.line(points, fill=color(), width=width)
        return image
    return drawer
Esempio n. 9
0
def text(fonts, font_sizes=None, drawings=None, color='#5C87B2',
         squeeze_factor=0.8):
    fonts = tuple([truetype(name, size)
                   for name in fonts
                   for size in font_sizes or (65, 70, 75)])
    if not callable(color):
        c = getrgb(color)

        def color():
            return c

    def drawer(image, text):
        draw = Draw(image)
        char_images = []
        for c in text:
            font = random.choice(fonts)
            c_width, c_height = draw.textsize(c, font=font)
            char_image = Image.new('RGB', (c_width, c_height), (0, 0, 0))
            char_draw = Draw(char_image)
            char_draw.text((0, 0), c, font=font, fill=color())
            char_image = char_image.crop(char_image.getbbox())
            for drawing in drawings:
                char_image = drawing(char_image)
            char_images.append(char_image)
        width, height = image.size
        offset = int((width - sum(int(i.size[0] * squeeze_factor)
                                  for i in char_images[:-1]) -
                      char_images[-1].size[0]) / 2)
        for char_image in char_images:
            c_width, c_height = char_image.size
            mask = char_image.convert('L').point(lambda i: i * 1.97)
            image.paste(char_image,
                        (offset, int((height - c_height) / 2)),
                        mask)
            offset += int(c_width * squeeze_factor)
        return image
    return drawer