【python教程】图像(image)的读取(Opencv/PIL/Scipy/Matplotlib)

 

目录

方法一:利用PIL中的 Image函数

方法二:利用opencv_python读取 

方法三:利用matplotlib.image as maping读取

方法四:利用skimage读取

方法五:利用scipy读取

方法六:利用glob读取

 


方法一:利用PIL中的 Image函数

注:该函数读取的image,不是array格式;若需获得array格式,用np.asarry()或者np.array()转化

from PIL import Image
import numpy as np
 
img = Image.open(filename) 
img.show()    #display the picture
img.save('./test.png') #save the picture with another name and it is svaed to the same file                           
IMG_array = np.array(img)
print (IMG_array.shape)

方法二:利用opencv_python读取 

注:cv2.imread()读取出来的是array格式,如果是rgb三通道图,则读取的数据是三维的;

import cv2
IMG = cv2.imread('./test.png')
print (IMG.shape)

方法三:利用matplotlib.image as maping读取

注:maping读取出来的是array格式

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img = mpimg.imread(filename)
print (img.shape) #python3.0 should use ()
plt.imshow(img) # to show the picture

方法四:利用skimage读取

from skimage import io,data
img=data.lena()
io.imshow(img)

print(type(img))  #显示类型
print(img.shape)  #显示尺寸
print(img.shape[0])  #图片宽度
print(img.shape[1])  #图片高度
print(img.shape[2])  #图片通道数
print(img.size)   #显示总像素个数
print(img.max())  #最大像素值
print(img.min())  #最小像素值
print(img.mean()) #像素平均值

方法五:利用scipy读取

注:读取出来的图像是矩阵形式

import matplotlib.pyplot as plt
from scipy import misc
import scipy
img = misc.imread('.test.png')
scipy.misc.imsave('./test_1.png', img)
plt.imshow(img)

方法六:利用glob读取

注:glob.glob()函数,里面的通配符匹配,在Windows下是不区分大小写的,而在Linux下是区分大小写的

此外,读取的是文件名

import glob
 
list = glob.glob(‘*g’)
print(list)

输出:['dog.1012.jpg', 'dog.1013.jpg', 'dog.1014.jpg', 'dog.1015.jpg', 'dog.1016.jpg']

猜你喜欢

转载自blog.csdn.net/qq_40837542/article/details/103823849