FlanChanXwO commited on
Commit
f3fa724
·
verified ·
1 Parent(s): 8cc7438

Upload scripts/gen_phpwind9.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/gen_phpwind9.py +83 -0
scripts/gen_phpwind9.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """复刻 PHPWind 9.x PwGDCode 渲染逻辑,生成代表性验证码。
3
+ 依据 alibaba/phpwind: upload/src/library/utility/verifycode/PwGDCode.php + PwBaseCode.php
4
+ """
5
+ import math
6
+ import random
7
+ import sys
8
+
9
+ import numpy as np
10
+ from PIL import Image, ImageDraw, ImageFont
11
+
12
+ W, H = 150, 60
13
+ CHARSET = "1234567890" # PwBaseCode verifyType=1
14
+ FONT = "/System/Library/Fonts/Supplemental/Arial.ttf"
15
+
16
+
17
+ def rand_color():
18
+ return (random.randint(0, 255), random.randint(0, 120), random.randint(0, 255))
19
+
20
+
21
+ def rand_noise_color():
22
+ return (random.randint(50, 255), random.randint(50, 200), random.randint(25, 200))
23
+
24
+
25
+ def gen(out_path, code=None):
26
+ if code is None:
27
+ code = "".join(random.choice(CHARSET) for _ in range(4))
28
+
29
+ # 背景:白底 (PwGDCode isRandBackground=false -> 255)
30
+ img = Image.new("RGB", (W, H), (255, 255, 255))
31
+ d = ImageDraw.Draw(img)
32
+
33
+ # 逐字符绘制:随机字号14-20、随机角度-20~10、随机色
34
+ codeX = (W - 20) / 4
35
+ codeY = H // 2 + random.randint(5, 10)
36
+ for i, ch in enumerate(code):
37
+ size = random.randint(14, 20)
38
+ angle = random.randint(-20, 10)
39
+ font = ImageFont.truetype(FONT, size)
40
+ # 在更大的画布上画字再旋转,避免裁剪
41
+ tmp = Image.new("RGBA", (40, 40), (0, 0, 0, 0))
42
+ td = ImageDraw.Draw(tmp)
43
+ td.text((2, 2), ch, font=font, fill=rand_color() + (255,))
44
+ tmp = tmp.rotate(angle, expand=True, fillcolor=(0, 0, 0, 0))
45
+ x = int(codeX * i + 10)
46
+ y = int(codeY - tmp.height / 2)
47
+ img.paste(tmp.convert("RGB"), (x, y), tmp.split()[3])
48
+
49
+ # 噪声:随机 线/点/弧 (PwGDCode _setRandGraph)
50
+ d = ImageDraw.Draw(img)
51
+ mode = random.randint(1, 3)
52
+ if mode == 1: # 30-40 条随机线
53
+ for _ in range(random.randint(30, 40)):
54
+ x1, y1 = random.randint(0, W), random.randint(0, H)
55
+ d.line([(x1, y1), (random.randint(x1 - 10, x1 + 5), random.randint(y1 + 5, y1 + 20))], fill=rand_noise_color(), width=1)
56
+ elif mode == 2: # 600-800 随机点
57
+ for _ in range(random.randint(600, 800)):
58
+ d.point((random.randint(0, W - 1), random.randint(0, H - 1)), fill=rand_noise_color())
59
+ else: # 5-10 条弧
60
+ for _ in range(random.randint(5, 10)):
61
+ d.arc([random.randint(0, W), random.randint(10, H), random.randint(10, W), random.randint(H, H * 2)],
62
+ random.randint(0, 90), random.randint(0, 90), fill=rand_noise_color())
63
+
64
+ # 正弦扭曲:x += sin(y/height*2π-0.6)*5
65
+ arr = np.array(img)
66
+ out = np.zeros_like(arr)
67
+ for y in range(H):
68
+ shift = int(math.sin(y / H * 2 * math.pi - 0.6) * 5)
69
+ for x in range(W):
70
+ sx = x + shift
71
+ if 0 <= sx < W:
72
+ out[y, x] = arr[y, sx]
73
+ Image.fromarray(out).save(out_path)
74
+ return code
75
+
76
+
77
+ if __name__ == "__main__":
78
+ import os
79
+ outdir = sys.argv[1] if len(sys.argv) > 1 else "/tmp/sp_pw9"
80
+ os.makedirs(outdir, exist_ok=True)
81
+ for i in range(6):
82
+ code = gen(f"{outdir}/pw9_{i}.png")
83
+ print(f"pw9_{i}.png code={code}")