Python图片读显写的几种方式

一、读写路径 

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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值