如何利用Python 圖像庫完成各種圖像處理任務?
安裝了PIL后,我們現在可以轉到代碼了。首先,我們使用一些 matplotlib 函數。import matplotlib.image as img
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
將讀取以下圖像。它被命名為 image1.jpg。
# reading jpg image
img = img.imread('image1.jpg')
plt.imshow(img)
圖像被讀取。# modifying the shape of the image
lum1 = img[:, :, 0]
plt.imshow(lum1)
現在修改了圖像形狀。
現在我們將其更改為“熱”顏色圖。
plt.imshow(lum1, cmap ='hot')
plt.colorbar()
圖像輸出看起來:
現在我們嘗試不同的顏色圖。imgplot = plt.imshow(lum1)
imgplot.set_cmap('nipy_spectral')
圖像輸出:
使用顏色圖的原因是,通常在各種應用程序和用途中,擁有統一的顏色圖會有所幫助。
現在讓我們看看為什么我們將圖像稱為二維數組。#data type of lum1
print(type(lum1))
輸出:<class 'numpy.ndarray'>print(lum1)
[[ 92 91 89 … 169 168 169][110 110 110 … 168 166 167][100 103 108 … 164 163 164]…[ 97 96 95 … 144 147 147][ 99 99 98 … 145 139 138][102 102 103 … 149 137 137]]這些點只是為了表明它們之間還有更多的數據點。但是可以肯定的是,所有數據都是數字數據。讓我們找出數組的大小。len(lum1)
輸出:320len(lum1[300])
輸出:658這為我們提供了圖像的像素數和尺寸:320*658。我們稍后也會驗證這一點。現在,我們使用 PIL。from PIL import Image

請輸入評論內容...
請輸入評論/評論長度6~500個字