-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_captcha_images.py
73 lines (59 loc) · 2.12 KB
/
generate_captcha_images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import random
import string
from PIL import Image, ImageDraw, ImageFont
from perlin_noise import PerlinNoise
import numpy as np
# Define captcha dimensions
width = 200
height = 80
# Define captcha length
captcha_length = 6
# Define Perlin noise parameters
noise = PerlinNoise(octaves=4, seed=10)
scale = 20.0
# Define font
font1 = ImageFont.truetype("georgia.ttf", size=40)
font2 = ImageFont.truetype("impact.ttf", size=40)
font3 = ImageFont.truetype("tahoma.ttf", size=40)
# Define output folder
output_folder = "self_generated_captcha_images"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Generate 10000 captcha images
for i in range(10000):
# Generate random captcha text
captcha_text = "".join(
random.choices(string.ascii_uppercase + string.digits, k=captcha_length)
)
# Create a new image
image = Image.new("RGB", (width, height), color=(255, 255, 255))
# Get a drawing context
draw = ImageDraw.Draw(image)
# Add Perlin noise to the image
perlin_noise = np.zeros((height, width))
for y in range(height):
for x in range(width):
perlin_noise[y][x] = noise([x / scale, y / scale, i / scale])
perlin_noise = np.uint8(
np.interp(perlin_noise, (np.min(perlin_noise), np.max(perlin_noise)), (0, 255))
)
noise_image = Image.fromarray(perlin_noise)
noise_image = noise_image.convert("RGB")
# print("Image Size: ", image.mode)
# print("Noise Image Size: ", noise_image.mode)
# breakpoint()
image = Image.blend(image, noise_image, 0.5)
# Add captcha text to the image
for j, c in enumerate(captcha_text):
# Incline the digit randomly
angle = random.randint(-10, 10)
digit = Image.new("RGBA", (50, 50), color=(255, 255, 255, 0))
digit_draw = ImageDraw.Draw(digit)
digit_draw.text(
(0, 0), c, font=random.choice([font1, font2, font3]), fill=(0, 0, 0, 255)
)
digit = digit.rotate(angle, expand=True)
image.paste(digit, (20 + j * 30, 10), digit)
# Save the image
image.save(os.path.join(output_folder, "{}.png".format(captcha_text)))