Python隨機生成圖片


Python隨機生成圖片

 

Python生成隨機驗證碼,需要使用PIL模塊.

安裝:

1 pip install pillow

基本使用

1. 創建圖片

1 from PIL import Image
2 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
3  
4 # 在圖片查看器中打開
5 # img.show() 
6  
7 # 保存在本地
8 with open('code.png','wb') as f:
9     img.save(f,format='png'

 

2. 創建畫筆,用於在圖片上畫任意內容

1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
2 draw = ImageDraw.Draw(img, mode='RGB')

 

3. 畫點

1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
2 draw = ImageDraw.Draw(img, mode='RGB')
3 # 第一個參數:表示坐標
4 # 第二個參數:表示顏色
5 draw.point([100, 100], fill="red")
6 draw.point([300, 300], fill=(255, 255, 255))
 

4. 畫線

1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
2 draw = ImageDraw.Draw(img, mode='RGB')
3 # 第一個參數:表示起始坐標和結束坐標
4 # 第二個參數:表示顏色
5 draw.line((100,100,100,300), fill='red')
6 draw.line((100,100,300,100), fill=(255, 255, 255))
 

5. 畫圓

1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
2 draw = ImageDraw.Draw(img, mode='RGB')
3 # 第一個參數:表示起始坐標和結束坐標(圓要畫在其中間)
4 # 第二個參數:表示開始角度
5 # 第三個參數:表示結束角度
6 # 第四個參數:表示顏色
7 draw.arc((100,100,300,300),0,90,fill="red")

6. 寫文本

1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
2 draw = ImageDraw.Draw(img, mode='RGB')
3 # 第一個參數:表示起始坐標
4 # 第二個參數:表示寫入內容
5 # 第三個參數:表示顏色
6 draw.text([0,0],'python',"red")

7. 特殊字體文字

 1 img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
 2 draw = ImageDraw.Draw(img, mode='RGB')
 3 # 第一個參數:表示字體文件路徑
 4 # 第二個參數:表示字體大小
 5 font = ImageFont.truetype("kumo.ttf", 28)
 6 # 第一個參數:表示起始坐標
 7 # 第二個參數:表示寫入內容
 8 # 第三個參數:表示顏色
 9 # 第四個參數:表示顏色
10 draw.text([0, 0], 'python', "red", font=font)

圖片驗證碼

 1 import random
 2  
 3 def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
 4     code = []
 5     img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
 6     draw = ImageDraw.Draw(img, mode='RGB')
 7  
 8     def rndChar():
 9         """
10         生成隨機字母   
11         :return:
12         """
13         return chr(random.randint(65, 90))
14  
15     def rndColor():
16         """
17         生成隨機顏色
18         :return:
19         """
20         return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
21  
22     # 寫文字
23     font = ImageFont.truetype(font_file, font_size)
24     for i in range(char_length):
25         char = rndChar()
26         code.append(char)
27         h = random.randint(0, 4)
28         draw.text([i * width / char_length, h], char, font=font, fill=rndColor())
29  
30     # 寫干擾點
31     for i in range(40):
32         draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
33  
34     # 寫干擾圓圈
35     for i in range(40):
36         draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
37         x = random.randint(0, width)
38         y = random.randint(0, height)
39         draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
40  
41     # 畫干擾線
42     for i in range(5):
43         x1 = random.randint(0, width)
44         y1 = random.randint(0, height)
45         x2 = random.randint(0, width)
46         y2 = random.randint(0, height)
47  
48         draw.line((x1, y1, x2, y2), fill=rndColor())
49  
50     img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
51     return img,''.join(code)
52  
53  
54 if __name__ == '__main__':
55     # 1. 直接打開
56     # img,code = check_code()
57     # img.show()
58  
59     # 2. 寫入文件
60     # img,code = check_code()
61     # with open('code.png','wb') as f:
62     #     img.save(f,format='png')
63  
64     # 3. 寫入內存(Python3)
65     # from io import BytesIO
66     # stream = BytesIO()
67     # img.save(stream, 'png')
68     # stream.getvalue()
69  
70     # 4. 寫入內存(Python2)
71     # import StringIO
72     # stream = StringIO.StringIO()
73     # img.save(stream, 'png')
74     # stream.getvalue()
75  
76     pass

鏈接:https://pan.baidu.com/s/1yMYEC1SwI3IoRGeHFRxoYQ
提取碼:m6ww(注意:字體文件下載)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM