- Python Pillow 教程
- Python Pillow - 首頁
- Python Pillow - 概述
- Python Pillow - 環境搭建
- 基本影像操作
- Python Pillow - 影像處理
- Python Pillow - 調整影像大小
- Python Pillow - 翻轉和旋轉影像
- Python Pillow - 裁剪影像
- Python Pillow - 為影像新增邊框
- Python Pillow - 識別影像檔案
- Python Pillow - 合併影像
- Python Pillow - 剪下和貼上影像
- Python Pillow - 滾動影像
- Python Pillow - 在影像上寫入文字
- Python Pillow - ImageDraw 模組
- Python Pillow - 合併兩張影像
- Python Pillow - 建立縮圖
- Python Pillow - 建立水印
- Python Pillow - 影像序列
- Python Pillow 顏色轉換
- Python Pillow - 影像上的顏色
- Python Pillow - 建立彩色影像
- Python Pillow - 將顏色字串轉換為 RGB 顏色值
- Python Pillow - 將顏色字串轉換為灰度值
- Python Pillow - 透過更改畫素值來更改顏色
- 影像處理
- Python Pillow - 降噪
- Python Pillow - 更改影像模式
- Python Pillow - 影像合成
- Python Pillow - 使用 Alpha 通道
- Python Pillow - 透視變換應用
- 影像濾鏡
- Python Pillow - 為影像新增濾鏡
- Python Pillow - 卷積濾鏡
- Python Pillow - 模糊影像
- Python Pillow - 邊緣檢測
- Python Pillow - 浮雕影像
- Python Pillow - 增強邊緣
- Python Pillow - 銳化蒙版濾鏡
- 影像增強和校正
- Python Pillow - 增強對比度
- Python Pillow - 增強銳度
- Python Pillow - 增強色彩
- Python Pillow - 校正色彩平衡
- Python Pillow - 去噪
- 影像分析
- Python Pillow - 提取影像元資料
- Python Pillow - 識別顏色
- 高階主題
- Python Pillow - 建立動畫 GIF
- Python Pillow - 批次處理影像
- Python Pillow - 轉換影像檔案格式
- Python Pillow - 為影像新增填充
- Python Pillow - 顏色反轉
- Python Pillow 與 NumPy 的結合使用
- Python Pillow 與 Tkinter 的 BitmapImage 和 PhotoImage 物件結合使用
- Image 模組
- Python Pillow - 影像混合
- Python Pillow 有用資源
- Python Pillow - 快速指南
- Python Pillow - 函式參考
- Python Pillow - 有用資源
- Python Pillow - 討論
Python Pillow - 透視變換應用
在 Pillow 中應用透視變換涉及改變影像的視角或視點。這種變換以模擬視角變化的方式修改影像點的定位,例如旋轉、傾斜或扭曲影像。
Pillow 中的透視變換
Pillow 的 transform() 方法允許你對影像應用不同型別的變換。對於透視變換,使用 3x3 變換矩陣來定義變換。此矩陣可以表示旋轉、平移、縮放和剪下等操作。對於透視變換,矩陣元素控制每個畫素座標如何變化。
透視變換的矩陣元素
矩陣元素 (a, b, c, d, e, f, g, h) 定義變換:
a 和 d 代表 x 和 y 方向的縮放。
b 和 c 代表剪下或傾斜。
e 和 f 是沿 x 和 y 軸的平移。
g 和 h 是透視係數。
應用透視變換
我們可以直接使用 transform() 方法結合 Image.PERSPECTIVE,並傳遞四個源點和四個目標點來指定變換。
透視變換會顯著扭曲影像。因此,請仔細選擇變換矩陣或點,以獲得所需的效果,避免過度扭曲。
在 Pillow 中應用透視變換涉及使用變換矩陣或定義源點和目標點來改變影像的透視,模擬角度、方向或視點的變化。理解變換矩陣和點對映對於獲得所需的視覺效果至關重要,同時避免影像中出現過度扭曲或不需要的變換。
以下是 transform() 方法的語法和引數。
transformed_image = image.transform(size, method, data, resample=0, fill=0)
其中:
image − 我們要變換的 Pillow Image 物件。
size − 代表變換後圖像的輸出大小 (寬度, 高度) 的元組。
method − 定義要應用的變換型別,例如 Image.AFFINE、Image.PERSPECTIVE 或 Image.QUAD 等。
data − 根據所使用方法所需變換的資料。
resample (可選) − 要使用的重取樣方法,例如 Image.BILINEAR、Image.NEAREST 和 Image.BICUBIC。預設值為 0,即最近鄰。
fill (可選) − 變換後圖像邊界之外區域的填充顏色。預設值為 0,即黑色。
根據變換方法準備資料,對於 Image.AFFINE 變換,它需要一個 6 元素的元組來表示仿射變換矩陣。對於透視變換,使用 Image.PERSPECTIVE,它需要一個 8 元素的元組來表示透視變換矩陣。此外,Image.QUAD 涉及提供源和目標四邊形點以進行精確的透視對映。
示例
在這個例子中,我們使用 transform() 方法對給定的輸入影像執行透視變換。
from PIL import Image
import numpy
def find_data(source, target):
matrix = []
for s, t in zip(source, target):
matrix.append([t[0], t[1], 1, 0, 0, 0, -s[0]*t[0], -s[0]*t[1]])
matrix.append([0, 0, 0, t[0], t[1], 1, -s[1]*t[0], -s[1]*t[1]])
A = numpy.matrix(matrix, dtype=float)
B = numpy.array(source).reshape(8)
res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B)
return numpy.array(res).reshape(8)
#Open an image
image = Image.open("Images/logo_w.png")
# image.show()
#Define the transformation matrix for perspective transform
coeffs = find_data(
[(0, 0), (225, 0), (225, 225), (0, 225)],
[(15, 115), (140, 20), (140, 340), (15, 250)])
#Applying the perspective transformation
transformed_image = image.transform((300, 400), Image.PERSPECTIVE, coeffs, Image.BICUBIC)
#Save or display the transformed image
transformed_image.save("output Image/transform.png")
transformed_image.show()
輸出
以下是輸入影像:
執行上述程式碼後,將顯示以下變換後的影像:
示例
這是一個使用矩陣元素 (a, b, c, d, e, f, g, h) 對另一個影像執行透視變換的另一個示例。
from PIL import Image
#Open an image
image = Image.open("Images/flowers.jpg")
image.show()
#Define the transformation matrix for perspective transform
matrix = (10, 4, 11, -1, 5, 2, 1, -1)
#Apply the perspective transformation
transformed_image = image.transform(image.size, Image.PERSPECTIVE, matrix)
#Save or display the transformed image
transformed_image.save("output Image/transform_image.jpg")
transformed_image.show()
輸出
執行上述程式碼後,您將獲得以下輸出:
輸入影像
輸出透視變換後的影像
注意
transform() 方法支援多種變換型別,所需變換資料根據所選方法而異。
務必根據所選變換方法提供正確的變換資料(即矩陣或點),以達到預期效果。
Pillow 中的 transform() 方法用途廣泛,允許我們對影像執行不同型別的幾何變換,從而在影像處理和透視更改方面具有靈活性。