python显示图片列表_python读取图片任意范围区域
使用python進行圖片處理,現在需要讀出圖片的任意一塊區域,并將其轉化為一維數組,方便后續卷積操作的使用。
下面使用兩種方法進行處理:
convert 函數
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
def ImageToMatrix(filename):
im = Image.open(filename) # 讀取圖片
im.show() # 顯示圖片
width,height = im.size
print("width is :" + str(width))
print("height is :" + str(height))
im = im.convert("L") # pic --> mat 轉換,可以選擇不同的模式,下面有函數源碼具體說明
data = im.getdata()
data = np.matrix(data,dtype='float')/255.0
new_data = np.reshape(data * 255.0,(height,width))
new_im = Image.fromarray(new_data)
# 顯示從矩陣數據得到的圖片
new_im.show()
return new_data
def MatrixToImage(data):
data = data*255
new_im = Image.fromarray(data.astype(np.uint8))
return new_im
'''
convert(self, mode=None, matrix=None, dither=None, palette=0, colors=256)
| Returns a converted copy of this image. For the "P" mode, this
| method translates pixels through the palette. If mode is
| omitted, a mode is chosen so that all information in the image
| and the palette can be represented without a palette.
|
| The current version supports all possible conversions between
| "L", "RGB" and "CMYK." The **matrix** argument only supports "L"
| and "RGB".
|
| When translating a color image to black and white (mode "L"),
| the library uses the ITU-R 601-2 luma transform::
|
| L = R * 299/1000 + G * 587/1000 + B * 114/1000
|
| The default method of converting a greyscale ("L") or "RGB"
| image into a bilevel (mode "1") image uses Floyd-Steinberg
| dither to approximate the original image luminosity levels. If
| dither is NONE, all non-zero values are set to 255 (white). To
| use other thresholds, use the :py:meth:`~PIL.Image.Image.point`
| method.
|
| :param mode: The requested mode. See: :ref:`concept-modes`.
| :param matrix: An optional conversion matrix. If given, this
| should be 4- or 12-tuple containing floating point values.
| :param dither: Dithering method, used when converting from
| mode "RGB" to "P" or from "RGB" or "L" to "1".
| Available methods are NONE or FLOYDSTEINBERG (default).
| :param palette: Palette to use when converting from mode "RGB"
| to "P". Available palettes are WEB or ADAPTIVE.
| :param colors: Number of colors to use for the ADAPTIVE palette.
| Defaults to 256.
| :rtype: :py:class:`~PIL.Image.Image`
| :returns: An :py:class:`~PIL.Image.Image` object.
'''
原圖:
filepath = "./imgs/"
imgdata = ImageToMatrix("./imgs/0001.jpg")
print(type(imgdata))
print(imgdata.shape)
plt.imshow(imgdata) # 顯示圖片
plt.axis('off') # 不顯示坐標軸
plt.show()
運行結果:
mpimg 函數
import matplotlib.pyplot as plt # plt 用于顯示圖片
import matplotlib.image as mpimg # mpimg 用于讀取圖片
import numpy as np
def readPic(picname, filename):
img = mpimg.imread(picname)
# 此時 img 就已經是一個 np.array 了,可以對它進行任意處理
weight,height,n = img.shape #(512, 512, 3)
print("the original pic: \n" + str(img))
plt.imshow(img) # 顯示圖片
plt.axis('off') # 不顯示坐標軸
plt.show()
# 取reshape后的矩陣的第一維度數據,即所需要的數據列表
img_reshape = img.reshape(1,weight*height*n)[0]
print("the 1-d image data :\n "+str(img_reshape))
# 截取(300,300)區域的一小塊(12*12*3),將該區域的圖像數據轉換為一維數組
img_cov = np.random.randint(1,2,(12,12,3)) # 這里使用np.ones()初始化數組,會出現數組元素為float類型,使用np.random.randint確保其為int型
for j in range(12):
for i in range(12):
img_cov[i][j] = img[300+i][300+j]
img_reshape = img_cov.reshape(1,12*12*3)[0]
print((img_cov))
print(img_reshape)
# 打印該12*12*3區域的圖像
plt.imshow(img_cov)
plt.axis('off')
plt.show()
# 寫文件
# open:以append方式打開文件,如果沒找到對應的文件,則創建該名稱的文件
with open(filename, 'a') as f:
f.write(str(img_reshape))
return img_reshape
if __name__ == '__main__':
picname = './imgs/0001.jpg'
readPic(picname, "data.py")
讀出的數據(12*12*3),每個像素點以R、G、B的順序排列,以及該區域顯示為圖片的效果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的python显示图片列表_python读取图片任意范围区域的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单目相机 svd 从图像恢复3维位置_论
- 下一篇: C++ STL 容器的一些总结