cv2方式:
# -*- coding: UTF-8 -*-
import cv2
"""
cv2模块--图片的读入和显示
"""
image_path="D:/PycharmProjects/imageCut/cutted_images/0.jpg"
img=cv2.imread(image_path)# np.ndarray BGR uint8
cv2.imshow("test_imread",img)# 转为RGB显示
cv2.waitKey()
PIL方式:
# -*-coding: UTF-8 -*-
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
image_path="D:/PycharmProjects/imageCut/cutted_images/0.jpg"
#PIL
#相关:scipy.misc.imread, scipy.ndimage.imread
#misc.imread 提供可选参数mode,但本质上是调用PIL,具体的模式可以去看srccode或者document
#https://github.com/scipy/scipy/blob/v0.17.1/scipy/misc/pilutil.py
img=Image.open(image_path)
img=np.array(img)# 获得numpy对象, np.ndarray, RGB
#统一使用plt进行显示,不管是plt还是cv2.imshow,在python中只认numpy.array,但是由于cv2.imread 的图片是BGR,cv2.imshow 时相应的换通道显示
plt.imshow(img)
plt.show()
matplotlib.image:
# -*-coding: UTF-8 -*-
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image_path="D:/PycharmProjects/imageCut/cutted_images/0.jpg"
img=mpimg.imread(image_path)
#统一使用plt进行显示,不管是plt还是cv2.imshow,在python中只认numpy.array,但是由于cv2.imread 的图片是BGR,cv2.imshow 时相应的换通道显示
plt.imshow(img)
plt.show()
skimage.io方式:
# -*-coding: UTF-8 -*-
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
image_path="D:/PycharmProjects/imageCut/cutted_images/0.jpg"
#4 skimge
#caffe.io.load_iamge()也是调用的skimage实现的,返回的是0-1 float型数据
img = io.imread(image_path)
#统一使用plt进行显示,不管是plt还是cv2.imshow,在python中只认numpy.array,但是由于cv2.imread 的图片是BGR,cv2.imshow 时相应的换通道显示
plt.imshow(img)
plt.show()
scipy.misc方式:
# -*-coding: UTF-8 -*-
from scipy.misc import imread, imshow, imsave
import matplotlib.pyplot as plt
image_path="D:/PycharmProjects/imageCut/cutted_images/0.jpg"
img = imread(image_path)
# imshow(img) need to set the SCIPY_PIL_IMAGE_VIEWER environment variable to point to the program/executable that can be used to view images on your system
plt.imshow(img)
plt.show()
imsave("D:/PycharmProjects/imageCut/cutted_images/1.jpg",img)