生成RBG三色
# 隨機生成RGB的色彩值
def rgbcolor():
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)
return red,green,blue
生成隨機的數據(子母、數字)
# 生成隨機的子母(大寫小寫)和數字
def getRandomChar():
random_num = str(random.randint(0, 9))
random_lower = chr(random.randint(97, 122)) # 小寫字母a~z
random_upper = chr(random.randint(65, 90)) # 大寫字母A~Z
random_char = random.choice([random_num, random_lower, random_upper])
return random_char
創建圖片
生成背景圖
rgb_color = rgbcolor()
# 創建一張隨機的背景圖片
img = Image.new(mode="RGB",size=(width,height),color=rgb_color)
print(img)
img.show()
隨機的背景圖
在背景圖上繪制數據
def createCodeImage():
background_color = rgbcolor()
# 創建一張隨機的背景圖片
img = Image.new(mode="RGB",size=(width,height),color=background_color)
# 設置文字的字體
font = ImageFont.truetype(font="ahellya.ttf",size=36)
# 圖片畫筆進行繪制圖片
draw = ImageDraw.Draw(img)
# 隨機生成4位驗證碼
for index in range(5):
str_or_num = getRandomChar()
text_color = rgbcolor()
# 防止背景的顏色和字體的顏色一致
while text_color == background_color:
text_color = rgbcolor()
draw.text((10+30*index,3),text=str_or_num,fill=text_color,font=font)
img.show()
createCodeImage()
隨機生成的驗證碼
添加干擾項(線條、點)
繪制線條
# 繪制線條
def create_line(draw):
for i in range(10):
x1 = random.randint(0,width)
x2 = random.randint(0,width)
y1 = random.randint(0,height)
y2 = random.randint(0,height)
draw.line((x1,y1,x2,y2),fill=rgbcolor(),width=2)
繪制點
# 繪制隨機點
def create_point(draw):
for i in range(50):
x_point = random.randint(0,width)
y_point = random.randint(0,height)
draw.point((x_point,y_point),fill=rgbcolor())
代碼總
# 圖片驗證碼
import random
from PIL import Image, ImageDraw, ImageFont
# 生成隨機的子母(大寫小寫)和數字
def getRandomChar():
random_num = str(random.randint(0, 9))
random_lower = chr(random.randint(97, 122)) # 小寫字母a~z
random_upper = chr(random.randint(65, 90)) # 大寫字母A~Z
random_char = random.choice([random_num, random_lower, random_upper])
return random_char
# 隨機生成RGB的色彩值
def rgbcolor():
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
return red, green, blue
# 創建圖片的高度和寬度
width = 160
height = 50
# 繪制線條
def create_line(draw):
for i in range(10):
x1 = random.randint(0, width)
x2 = random.randint(0, width)
y1 = random.randint(0, height)
y2 = random.randint(0, height)
draw.line((x1, y1, x2, y2), fill=rgbcolor(), width=2)
# 繪制隨機點
def create_point(draw):
for i in range(50):
x_point = random.randint(0, width)
y_point = random.randint(0, height)
draw.point((x_point, y_point), fill=rgbcolor())
def createCodeImage():
background_color = rgbcolor()
# 創建一張隨機的背景圖片
img = Image.new(mode="RGB", size=(width, height), color=background_color)
# 設置文字的字體
font = ImageFont.truetype(font="ahellya.ttf", size=36)
# 圖片畫筆進行繪制圖片
draw = ImageDraw.Draw(img)
# 隨機生成5位驗證碼
str_data = ""
for index in range(5):
str_or_num = getRandomChar()
text_color = rgbcolor()
# 防止背景的顏色和字體的顏色一致
while text_color == background_color:
text_color = rgbcolor()
draw.text((10 + 30 * index, 3), text=str_or_num, fill=text_color, font=font)
str_data += str_or_num
print(str_data) # 生成的驗證碼
create_line(draw)
create_point(draw)
img.show()
createCodeImage()
效果如下所示: