標籤:ext dom ttf 產生 畫圓 for 畫線 儲存 特殊
1 驗證基礎知識
1.1 Python產生隨機驗證碼,需要使用PIL模組.
# 安裝pip3 install pillow
1.2 建立圖片
from PIL import Imageimg = Image.new(mode="RGB", size=(120, 30), color=(125, 255, 255))# 儲存圖片到本地with open("code.png",‘wb‘) as f:img.save(f,format="png")
1.3 建立畫筆
from PIL import Imagefrom PIL import ImageDrawimg = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))draw = ImageDraw.Draw(img, mode="RGB")
1.4 畫點
from PIL import Imagefrom PIL import ImageDrawimg = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))draw = ImageDraw.Draw(img, mode="RGB")# 第一個參數:表示座標# 第二個參數:表示顏色draw.point([100,20], fill="red")draw.point([50,10], fill=(255, 135, 255))with open("code.png",‘wb‘) as f:img.save(f,format="png")
1.5 畫線
from PIL import Imagefrom PIL import ImageDrawimg = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))draw = ImageDraw.Draw(img, mode="RGB")# 第一個參數:表示起始座標和結束座標# 第二個參數:表示顏色draw.line((10, 10, 10, 30), fill="red")draw.line((10, 10, 30, 10), fill=(255, 135, 255))with open("code.png",‘wb‘) as f:img.save(f,format="png")
1.6 畫圓
from PIL import Imagefrom PIL import ImageDrawimg = Image.new(mode="RGB", size=(150, 150), color=(255,255,255))draw = ImageDraw.Draw(img, mode="RGB")# 第一個參數:表示起始座標和結束座標(圓要畫在中間)# 第二個參數:表示開始角度# 第三個參數:表示結束角度# 第四個參數:表示顏色draw.arc((50, 50, 100, 100), 0, 360, fill="red")with open("code.png",‘wb‘) as f:img.save(f,format="png")
1.7 寫文本
from PIL import Imagefrom PIL import ImageDrawimg = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))draw = ImageDraw.Draw(img, mode="RGB")# 第一個參數:表示起始座標# 第二個參數:表示寫入內容# 第三個參數:表示顏色draw.text([20, 10], "python", fill="red")with open("code.png",‘wb‘) as f:img.save(f,format="png")
1.8 設定特殊字型
from PIL import Image, ImageDraw, ImageFontimg = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))draw = ImageDraw.Draw(img, mode="RGB")# 第一個參數:表示字型路徑# 第二個參數:表示字型大小font = ImageFont.truetype("kumo.ttf", 25)# 第一個參數:表示起始座標# 第二個參數:表示寫入內容# 第三個參數:表示顏色# 第四個參數:表示字型draw.text([30, 0], "python", fill="red", font=font)with open("code.png",‘wb‘) as f:img.save(f,format="png")
2 應用
2.1 應用方法
import randomfrom io import BytesIOfrom PIL import Image, ImageDraw, ImageFontdef check_code(width=120, height=30, char_length=5, font_file=‘kumo.ttf‘, font_size=28): f = BytesIO() img = Image.new(mode=‘RGB‘, size=(width, height), color=(random.randint(50, 250), random.randint(50, 255), random.randint(50, 255))) draw = ImageDraw.Draw(img, mode=‘RGB‘) char_list = [] # 畫字 for i in range(char_length): char = random.choice([chr(random.randint(65, 90)), str(random.randint(1, 9)), chr(random.randint(97, 122)), ]) font = ImageFont.truetype("blog/static/fonts/"+font_file, font_size) draw.text([i * 24, 0], char, (random.randint(200, 255), random.randint(0, 50), random.randint(0, 50)), font=font) char_list.append(char) def rndColor(): """ 產生隨機顏色 :return: """ return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255)) # 寫幹擾點 for i in range(40): draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor()) # 寫幹擾圓圈 for i in range(40): draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor()) x = random.randint(0, width) y = random.randint(0, height) draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor()) # 畫幹擾線 for i in range(5): x1 = random.randint(0, width) y1 = random.randint(0, height) x2 = random.randint(0, width) y2 = random.randint(0, height) draw.line((x1, y1, x2, y2), fill=rndColor()) img.save(f, "png") data = f.getvalue() s_code = ‘‘.join(char_list) return data,s_code
2.2 調用方法
def get_valid_img(request): ‘‘‘ 登入和註冊的驗證碼 :param request: :return: ‘‘‘ data,s_code = check_code() request.session["valid_code"] = s_code return HttpResponse(data)
python實現圖片驗證碼