Home > Article > Backend Development > A brief introduction to image reading and writing methods of various image libraries in Python (with code)
This article brings you a brief introduction to the image reading and writing methods of various image libraries in Python (with code). It has certain reference value. Friends in need can refer to it. I hope It will help you.
There are currently several mainstream image libraries:
1. OpenCV 2. PIL (Pillow) 3. matplotlib.image 4.skimage 5. scipy.misc
Conclusion : Among the above image libraries, OpenCV is undoubtedly the most powerful and mature.
1.1 OpenCV image reading and storage
import cv2 #读取图像 直接是numpy矩阵格式 img = cv2.imread('horse.jpg',1) # 0表示读入灰色图片,1表示读入彩色图片 cv2.imshow('image',img) # 显示图像 print(img.shape) # (height,width,channel) print(img.size) # 像素数量 print(img.dtype) # 数据类型 print(img) # 打印图像的numpy数组,3纬数组 #储存图像 # 当前目录储存 cv2.write(‘horse1.jpg',img) # 自定义储存 cv2.write(‘/path_name/’ + str(image_name) + '.jpg',img) cv2.waitKey()
1.2OpenCV image graying processing
import cv2 #方法一 img = cv2.imread('horse.jpg',0) # 0表示读入灰色图片,或者使用cv2.IMREAD_GRATSCALE 替代0 cv2.imshow('gray image',img) #方法二 img = cv2.imread('horse.jpg') gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.imshow('gray image',gray_img) print(gray_img.shape) # (height, width) print(gray_img.size) # 像素数量 print(gray_img) # 打印图像的numpy数组,2维 cv2.waitKey()
1.3 OpenCV matrix format transformation
Why? :OpenCV’s matrix format (height, width, channels) -->> The deep learning matrix type may be (channels, height, width)
import cv2 import numpy as np img = cv2.imread('horse.jpg',1) cv2.imshow('image',img) # 矩阵格式的变换 print(img.shape) img = img.transpose(2,0,1) #变换函数 print(img.shape)
# 矩阵扩展 (batch_size, channels, height, width) 预测单张图片的操作 # 加一列作为图片的个数 img = np.expand_dims(img, axis=0) #使用numpy函数 print(img.shape)
# 训练阶段构建batchdata_lst = [] loop: img = cv2.imread('xxx.jpg') data_lst.append(img) data_arr = np.array(data_lst)
1.4 OpenCV image normalization (Data Normalization)
import cv2 # 为了减少计算量,需要把像素值0-255转换到0-1之间 img = cv2.imread('horse.jpg') img = img.astype('float') / 255.0 # 先转化数据类型为float print(img.dtype) print(img)
1.5 OpenCV BRG to RGB
import cv2 img = cv2.imread('horse.jpg') img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # 转为RGB format print(img)
1.6 OpenCV access pixels
import cv2 img = cv2.imread('horse.jpg') gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 转为Gray image print(img[4,4]) # 3 channels print(gray_img[4,4]) # 1 channel
1.7 OpenCV region of interest clipping (ROI)
import cv2 img = cv2.imread('horse.jpg') print(img.shape) roi = img[0:437,0:400] # [y:height,x:width] cv2.imshow('roi',roi) cv2.waitKey()
2.1 PIL image Reading and storage
from PIL import Image import numpy as np #图像读取 img = Image.open('horse.jpg') print(img.format) # 图片格式 print(img.size) # (width,height) print(img.mode) # 图片通道类型 #将图像转化为矩阵格式 arr = np.array(img) print(arr.shape) print(arr.dtype) #图像储存 new_img = Image.fromarray(arr) new_img.save('test.jpg') img.show()
2.2 PIL image graying processing
#图像灰化处理 gray = Image.open('horse.jpg').convert('L') gray_arr = np.array(gray) print(gray_arr.shape) # (height,width) print(gray_arr.dtype) print(gray_arr) gray.show()
2.3 PIL area of interest shearing
# 感兴趣区域剪切 img = Image.open('horse.jpg') roi = img.crop((0,0,200,200)) # (左上x,左上y,右下x,右下y) roi.show()
2.4 Channel operation
# 通道处理 r,g,b = img.split() #分离 img = Image.merge("RGB",(b,g,r)) #合并 img = img.copy() #复制
3.1 Matplotlib reads and stores images
import matplotlib.pyplot as plt import numpy as np # 图像读取为numpy数组格式 img = plt.imread('horse.jpg') plt.axis('off') # 关闭刻度显示 print(img.shape) # (height, width, channel) print(img.size) # 像素数量 print(img.dtype) #储存图片 plt.savefig('./name.jpg') figure = plt.figure(figsize=(20,10)) # 调整显示图片的大小 plt.imshow(img) plt.show()
3.2 Matplotlib image graying processing
#图片灰化处理 # 平均值发 img_mean = img.mean(axis=2) plt.imshow(img_mean,cmap='gray') plt.show() #最大值法 img_max = img.max(axis=-1) plt.imshow(img_max,cmap='gray') plt.show() #RGB三原色法 gravity = np.array([0.299,0.587,0.114]) img_gravity = np.dot(img,gravity) plt.imshow(img_gravity,cmap="gray") plt.show()
4.1 skimage reads and stores images
from skimage import io #读取图像numpy数组格式 img = io.imread('horse.jpg') print(img.shape) print(img.dtype) print(img.size) #print(img) io.imshow(img) #储存图像 io.imsave('test.jpg',img)
4.2 skimage graying processing
#图像灰化处理并归一化 img = io.imread('horse.jpg',as_gray=True) print(img.shape) print(img.dtype) # 数据类型位float print(img.size) print(img) io.imshow(img) io.show()
5.1 scipy.misc Reading and storing images
#在1.2.0 之后统一用imageio模块 import imageio import matplotlib.pyplot as plt #读取图片为numpy数组 img = imageio.imread('horse.jpg') print(img.dtype) print(img.size) # 像素数量 print(img.shape) #(height, width, channels) plt.imshow(img) plt.show() print(img) #储存图片 imageio.imsave('test.jpg',img)
The above is the detailed content of A brief introduction to image reading and writing methods of various image libraries in Python (with code). For more information, please follow other related articles on the PHP Chinese website!