Python的imread()函数

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)

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值