今天我們要學習的內容是如何利用Python生成一個隨機的中文驗證碼,並將圖片保存為.jpeg格式,需要的朋友可以參考下
在這之前,你首先得了解Python中的PIL函式庫。 PIL是Python Imaging Library的簡稱,PIL是一個Python處理圖片的庫,提供了一系列模組和方法,例如:裁剪,平移,旋轉,改變尺寸等等。在PIL庫中,任何一個圖像都是用Image物件來表示的,所以要載入一張圖片,最簡單的形式如下:
##
from PIL import Image image = Image.open("1.jpeg")在PIL函式庫中,最常用的模組有Image,ImageDraw,ImageEnhance,ImageFile等。
PIL的下載網址為:
http://www.pythonware.com/products/pil/
(1)改變圖片大小
from PIL import Image image = Image.open("1.jpeg") new_img = image.resize((256,256),Image.BILINEAR) new_img.save("2.jpeg")(2)旋轉圖片
from PIL import Image image = Image.open("1.jpeg") new_img = image.rotate(45) new_img.save("2.jpeg")
(3)直線繪製PIL庫中的ImageDraw模組提供了圖形繪製的基本功能,可以繪製直線,弧線,橢圓,矩形等等。
from PIL import Image,ImageDraw image = Image.open("2.jpeg") draw = ImageDraw.Draw(image) width,height = image.size draw.line(((0,0),(width-1,height-1)),fill=255) draw.line(((0,height-1),(width-1,0)),fill=255) image.save("2.jpeg")(4)繪製圓形
from PIL import Image,ImageDraw image = Image.open("2.jpeg") draw = ImageDraw.Draw(image) width,height = image.size draw.arc((0,0,width-1,height-1),0,360,fill=255) image.save("2.jpeg")更多方法可以請參閱說明文檔。
# -*- coding: utf-8 -*- from PIL import Image,ImageDraw,ImageFont import random import math, string class RandomChar(): @staticmethod def Unicode(): val = random.randint(0x4E00, 0x9FBF) return unichr(val) @staticmethod def GB2312(): head = random.randint(0xB0, 0xCF) body = random.randint(0xA, 0xF) tail = random.randint(0, 0xF) val = ( head << 8 ) | (body << 4) | tail str = "%x" % val return str.decode('hex').decode('gb2312') class ImageChar: def __init__(self, fontColor = (0, 0, 0), size = (100, 40), fontPath = 'SIMSUN.TTC', bgColor = (255, 255, 255), fontSize = 20): self.size = size self.fontPath = fontPath self.bgColor = bgColor self.fontSize = fontSize self.fontColor = fontColor self.font = ImageFont.truetype(self.fontPath, self.fontSize) self.image = Image.new('RGB', size, bgColor) def rotate(self): self.image.rotate(random.randint(0, 30), expand=0) def drawText(self, pos, txt, fill): draw = ImageDraw.Draw(self.image) draw.text(pos, txt, font=self.font, fill=fill) def randRGB(self): return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) def randPoint(self): (width, height) = self.size return (random.randint(0, width), random.randint(0, height)) def randLine(self, num): draw = ImageDraw.Draw(self.image) for i in range(0, num): draw.line([self.randPoint(), self.randPoint()], self.randRGB()) def randChinese(self, num): gap = 5 start = 0 for i in range(0, num): char = RandomChar().GB2312() x = start + self.fontSize * i + random.randint(0, gap) + gap * i self.drawText((x, random.randint(-5, 5)), RandomChar().GB2312(), self.randRGB()) self.rotate() self.randLine(18) def save(self, path): self.image.save(path) ic = ImageChar(fontColor=(100,211, 90)) ic.randChinese(4) ic.save("1.jpeg")運行結果:

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

如何使用正則表達式匹配到第一個閉合標籤就停止?在處理HTML或其他標記語言時,常常需要使用正則表達式來�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),