圖像的RGB色彩模式
RGB三種顏色通道的變化和疊加得到各種顏色
- R紅色,取值範圍,0-255
- G綠色,取值範圍,0-255
- B顏色,取值範圍,0-255
RGB形成的顏色包括了人類視力所能感知的所有顏色
pillow庫安裝和導入
PIL是一個具有強大圖像處理能力的第三方庫
命令行安裝方法:pip install pillow
from PIL import Image
Image是PIL庫中代表一個圖像的類(對象)
圖像的數組表示
圖像是一個由像素組成的二維矩陣,每個元素是一個RGB值。
圖像是一個三維數組,維度分別表示高度、寬度和像素RGB值
from PIL import Image
import numpy as np
im = np.array(Image.open("1.jpg"))
print(im.shape)
print(im.dtype)
運行結果:
(331, 500, 3)
uint8
圖像的變換
讀入圖像後,獲取像素RGB值,修改後保存爲新的文件
im = np.array(Image.open("1.jpg"))
b = [255,255,255]-im
im = Image.fromarray(b.astype('uint8'))
im.save("2.jpg")
運行前後的文件是:
im = np.array(Image.open("1.jpg").convert('L'))
b = 255-im
im = Image.fromarray(b.astype('uint8'))
im.save("3.jpg")
convert(‘L’)將一個彩色的圖片變成一個灰度的圖片
每一個RGB的值對應一個灰度值
變換後前的圖像爲:
進行區間變換
a = np.array(Image.open("1.jpg").convert('L'))
c = (100/255)*a + 150
im = Image.fromarray(c.astype('uint8'))
im.save("4.jpg")
變換前後的圖像爲:
同樣地,還有類似變換:
a = np.array(Image.open("1.jpg").convert('L'))
c = 255*(a/255)**2 # 像素平方
im = Image.fromarray(c.astype('uint8'))
im.save("5.jpg")
變換後的圖像爲:
修改一行代碼可以做任意變換
圖像的手繪效果變換
手繪效果的幾個特徵:
- 黑白灰色
- 邊界線條較重
- 相同或相近色彩趨於白色
- 略有光源效果
原理:梯度的重構
利用像素之間的梯度值和虛擬深度值對圖像進行重構
根據灰度變化來模擬人力視覺的明暗程度