PIL庫學習筆記
- ImageDraw模塊
- 兩種繪圖程式演示
- ImageFont模塊
- 文字程式演示
- 程式一:繪制十字
- 程式二:生成隨機驗證碼
- 程式三:繪制九宮格
- 程式四:修改特定像素點顏色
ImageDraw模塊
- ImageDraw模塊實作了繪圖功能
- 通過創建圖片的方式來繪制2D影像,還可以在原有圖片上進行繪圖,以達到修飾圖片或者對圖片進行注釋的目的
- 在ImageDraw模塊繪圖時需要首先創建一個ImageDraw.Draw物件,傳入一個圖片實體
- 使用Draw物件方法進行繪圖
獲取Draw物件
img = Image.open("檔案路徑")
pen = ImageDraw.Draw(img)
(1) 繪制直線
def line(self, [x1,y1,x2,y2], fill=None, width=0, joint=None)
- 以(x1,y1)為起點,以(x2,y2)為終點,畫一條fill顏色的,寬為width的實線
(2) 繪制圓弧
def arc(self, [x1,y1,x2,y2], start, end, fill=None, width=1)
- 在左上角坐標為(x1,y1),右下角坐標為(x2,y2)的矩形區域內
- 滿圓 O 內,以 start 為 起始角度,以 end 為終止角度,截取圓 O 的一部分圓弧并畫出來
- 如果[x1,y1,x2,y2]區域不 是正方形,則在該區域內的最大橢圓中根據角度截取片段
- 引數 fill 和 width 與 line 方法相同
(3) 繪制橢圓
def ellipse(self, [x1,y1,x2,y2], fill=None, outline=None, width=1)
- 用法同 arc 類似,用于畫圓(或者橢圓)
- outline 表示只規定圓的顏色,
(4) 繪制弦
def chord(self, [x1,y1,x2,y2], start, end, fill=None, outline=None, width=1)
- 用法同 arc 類似,用于畫圓中從 start 到 end 的弦
- fill 表示弦與圓弧之間空間用指定顏 色填滿,設定為 outline 表示只規定弦線的顏色
(5) 繪制扇形
def pieslice(self, [x1,y1,x2,y2], start, end, fill=None, outline=None, width=1)
- 用法同 elipse 類似,用于畫起止角度間的扇形區域
- fill 表示將扇形區域用指定顏色填滿,設定為 outline 表示只用指定顏色描出區域輪廓
(6) 繪制多邊形
def polygon(self, [x1,y1,x2,y2], fill=None, outline=None)
- 根據坐標畫多邊形,Python 會根據第一個參量中的(x,y)坐標對,連接出整個圖形
- fill 表示將多邊形區域用指定顏色填滿,outline 只用于設定指定顏色描出區域輪廓
(7) 繪制矩形
def rectangle(self, [x1,y1,x2,y2], fill=None, outline=None, width=1)
- 在指定的區域內畫一個矩形,(x1,y1)表示矩形左上角的坐標,(x2,y2)表示矩形右下角的坐標
- fill 用于將矩形區域顏色填滿,outline 用于描出區域輪廓
(8) 繪制文字
def text(
self,
xy,
text,
fill=None,
font=None,
anchor=None,
spacing=4,
align="left",
direction=None,
features=None,
language=None,
):
- 在影像內添加文字,其中引數 position 是一個二元組,用于指定文字左上角的坐標
- text 表示要寫入的文字內容
- fill 表示文本的顏色
- font 必須為 ImageFont 中指定的 font 型別
- spacing 表示行之間的像素數
- align 表示位置“left”,“center”或“right”
- direction 表示文字的方向,它可以是’rtl’(從右到左),‘ltr’(從左到右)或’ttb’(從上到下),
(9) 繪制點
def point(self, xy, fill=None)
- 給定坐標處繪制點(單個像素)
兩種繪圖程式演示
【示例】創建圖片的方式來繪制
from PIL import ImageDraw,Image,ImageFont
img = Image.new("RGB", (300, 300))
pen = ImageDraw.Draw(img)
pen.rectangle((10,10,250,250),fill='black',outline='red')
img_font = ImageFont.truetype('SIMLI.TTF',20)
pen.text((20,20),'這是一個測驗', fill='white', font=img_font)
img.show()

【示例】在原圖上繪制一個橢圓
from PIL import Image,ImageDraw
img = Image.open('picture\cup.jpg')
pen = ImageDraw.Draw(img)
pen.arc((0,0,img.width-1,img.height-1),0,360,fill='blue',width=10)
img.show()

ImageFont模塊
- ImageFont莫苦熬的功能是實作對字體和字型的處理
(1)load函式
- 從
指定的檔案中加載一種字體,該函式回傳對應的字體物件 - 如果該函式運行失敗,那么將產生 IOError 例外
def load(filename)
(2)load_path函式
- 和函式 load()一樣,但是如果
沒有指定當前路徑,就會從檔案 sys.path 開 始查找指定的字體檔案
def load_path(filename)
(3)load_default函式
- 功能是加載一種
默認的字體
def load_default()
(4)getsize函式
- 回傳給定文本的
寬度和高度,回傳值是一個二元組
def getsize(self, text, direction=None, features=None, language=None, stroke_width=0)
(5)truetype函式
def truetype(font=None, size=10, index=0, encoding="", layout_engine=None)
- 有兩種定義格式:
第 1 種格式的功能是加載一個 TrueType 或者 OpenType 字體檔案,并且創建一個字體物件,在 Windows 系統中,如果指定的檔案不存在,加載器 就會順便看看 Windows 的字體目錄下它是否存在
Image.truetype(file,size)
第 2 種格式的功能是,加載一個 TrueType 或者 OpenType 字體檔案,并且創建一個字體 物件,
| 編碼方式(encoding) | 含義 |
|---|---|
| unic | Unicode |
| symb | MicrosoftSymbol |
| ADOB | Adobe Standard |
| ADBE | Adobe Expert |
| armn | Apple Roman |
Image.truetype(file,size,encoding=value)
文字程式演示
【示例】在原圖上面繪制文字
from PIL import ImageFont, ImageDraw, Image
img = Image.open('picture\cup.jpg')
pen = ImageDraw.Draw(img)
img_font = ImageFont.load_default()
pen.text((20, 10), 'ceshi', font=img_font, fill='white')
img_font = ImageFont.truetype('SIMYOU.TTF', 50)
pen.text((20, 80), '測驗', font=img_font, fill='red')
img_font = ImageFont.truetype('SIMYOU.TTF',50)
pen.text((20, 150), '測驗', font=img_font, fill='blue')
img.show()

程式一:繪制十字
繪制十字
from PIL import Image, ImageDraw
img = Image.open('picture\cup.jpg')
pen = ImageDraw.Draw(img)
pen.line([0, 0, img.width, img.height], fill='blue', width=5)
pen.line([0, img.height, img.width, 0], fill='red', width=5)
img.show()

程式二:生成隨機驗證碼
生成 隨機驗證碼
from PIL import Image, ImageDraw, ImageFont
import random
img = Image.new("RGB", (100, 100), "white")
pen = ImageDraw.Draw(img)
# 獲取一個隨機顏色
def get_color():
return (random.randint(200, 255), random.randint(200, 255), random.randint(200, 255))
# 獲取一個隨機大寫字符
def get_char():
return chr(random.randint(65, 90))
# 圖片每個點的顏色隨機
for x in range(img.width):
for y in range(img.height):
pen.point((x,y), fill=get_color())
# 在圖片上生成4個隨機字母
img_font = ImageFont.truetype('simsun.ttc', 36)
for i in range(4):
pen.text((10 + i * 20, 50), get_char(), font=img_font, fill="red")
# 干擾線
for i in range(2):
pen.line([10, 10, 80, 80], fill="green", width=4)
img.show()

程式三:繪制九宮格
繪制九宮格
from PIL import Image, ImageDraw
width = 300
height = 300
img = Image.new("RGB", (width, height), "blue")
pen = ImageDraw.Draw(img)
def get_color(x,y):
a = x // 100 + y // 100
if a == 0:
return (255, 0, 0)
if a == 1:
return (0, 255, 0)
if a == 2:
return (0, 0, 255)
if a == 3:
return (0, 0, 0)
if a == 4:
return (255, 255, 255)
for x in range(width):
for y in range(height):
pen.point((x,y), fill=get_color(x,y))
img.show()

程式四:修改特定像素點顏色
將圖片中所有黃色像素點修改成紅色像素點
from PIL import Image, ImageDraw
img = Image.open("picture\cup.jpg")
pen = ImageDraw.Draw(img)
# 正宗的黃色(255,255,0) 正宗紅色(255,0,0)
def modify_color(x,y):
old_color = img.getpixel((x,y))
if old_color[0] > 60 and old_color[1] > 60 and old_color[2] < 60 :
return (255, 0, 0)
else:
return old_color
for x in range(img.width):
for y in range(img.height):
pen.point((x,y), fill=modify_color(x, y))
img.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/248103.html
標籤:python
上一篇:tornado框架5分鐘快速上手、tornado基礎
下一篇:python隨機漫步
