1. img = img.convert()
PIL有九種不同模式: 1
,L
,P
,RGB
,RGBA
,CMYK
,YCbCr
,I
,F
。
1.1 img.convert('1')
為二值圖像,非黑即白。每個像素用8個bit表示,0表示黑,255表示白。
代碼示例
from PIL import Image
def convert_1():
image = Image.open("D:/pytorch_code/pytorch_study/fusion_datasets/1.jpg")
image_1 = image.convert('1')
image.show()
image_1.show()
1.2 img.convert('L')
轉化為灰度圖像,每個像素用8個bit表示,0表示黑,255表示白,其他數字表示不同的灰度。
轉換公式:L = R * 299/1000 + G * 587/1000+ B * 114/1000。
代碼示例
from PIL import Image
def convert_L():
image = Image.open("D:/pytorch_code/pytorch_study/fusion_datasets/1.jpg")
image_L = image.convert('L')
image.show()
image_L.show()
對比上圖可以發現,1
模式得到圖頓點很多,有點像高斯噪聲的感覺,而L
模式更平滑一些。
1.3 img.convert('P')
代碼示例
from PIL import Image
def convert_P():
image = Image.open("D:/pytorch_code/pytorch_study/fusion_datasets/1.jpg")
image_P = image.convert('P')
image.show()
image_P.show()