一、读写路径
import os
rootpath = os.getcwd() #当前文件夹路径,同文佳夹可直接读取
# rootpath = '' # 手动填写
readname = '/IDRiD_14.jpg'
writename = '/IDRiD_15.jpg'
readpath = os.path.normpath(rootpath + readname)
writepath = os.path.normpath(rootpath + writename)
二、相应库
2.1 Imageio库
2.1.1 读取图片
# 可直接填写上述 readpath
img = imageio.imread('test.png') # 常见图像格式
img = imageio.get_reader('cat.gif') # gif图像
video = imageio.get_reader('imageio:cockatoo.mp4')# 逐帧读取视频
# Grab screenshot or image from the clipboard
im_screen = imageio.imread('<screen>')
im_clipboard = imageio.imread('<clipboard>')
#Read medical data (DICOM)
dirname = 'path/to/dicom/files'
# Read as loose images
ims = imageio.mimread(dirname, 'DICOM')
# Read as volume
vol = imageio.volread(dirname, 'DICOM')
# Read multiple volumes (multiple DICOM series)
vols = imageio.mvolread(dirname, 'DICOM')
2.1.2 显示图片
import visvis as vv
vv.imshow(img) # 好像一运行图片窗口还没显示就消失了,待深究
2.1.3 保存/写出图片
img = np.array(img) # 写出图片可不必转换为np.array
imageio.imwrite(writepath, img)
2.2 Matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mplImage
img = mplImage.imread(readpath) # numpy.ndarray
plt.imshow(img)
plt.show() # SciView
plt.savefig(writepath) # 适用保存任何matplotlib画出的图像,类似screencapture
2.3 OpenCV-Python
img = cv2.imread(readpath) # numpy.ndarray, BGR顺序
h, l, d = img.shape; fscale = 0.2
cv2.namedWindow('image', 0)
cv2.resizeWindow('image', np.int(l*fscale), np.int(h*fscale))
cv2.imshow('image', img) # 自动适应图片大小的,不能缩放
key = cv2.waitKey(0)
if key == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif key == ord('s'): # wait for 's' key to save and exit
cv2.imwrite(writepath, img)
cv2.destroyAllWindows()
2.4 PIL
from PIL import Image
img = Image.open(readpath) # PIL.JpegImagePlugin.JpegImageFile
img.show() # 调用本地图片显示器
img.save(writepath)
2.5 skimage
from skimage import io,data
import matplotlib.pyplot as plt
img = io.imread(readpath) # numpy.ndarray
io.imshow(img)
plt.show()
io.imsave(writepath,img)
参考文献
1. https://imageio.readthedocs.io/en/latest/examples.html