python读取图像的几种方式

PIL库

参考:http://www.cnblogs.com/yinxiangnan-charles/p/5928689.html

import numpy as np
from PIL import Image
# 打开图像
im = Image.open('E:/liuying/Pictures/test.png')
im.show()
print("the datatype of im", type(im))  # 读取的数据类型
print("the size of im: ", im.size)  # 数据的大小
im_array = np.array(im)  # 转换为array数组
print("the datatype of im_array",type(im_array))

# 转换为灰度图像
im_gray = im.convert('L')
im_gray.show()

结果得到:

the datatype of im <class 'PIL.PngImagePlugin.PngImageFile'>
the size of im:  (203, 137)
the datatype of im_array <class 'numpy.ndarray'>

并排显示参考:https://blog.csdn.net/qq_21808961/article/details/80666589
这里会有个问题,就是在将显示的图片另存为的时候,只能保存格式BMP,有点疑惑,而且保存的图片无法正常查看。


skimage库

参考:https://www.jianshu.com/p/e8d058767dfa

from skimage import io
im = io.imread('E:/liuying/Pictures/test.png')
io.imshow(im)
print("the datatype of im", type(im))  # 读取的数据类型
print("the shape of img: ", im.shape)  # 数据的大小

结果:
在这里插入图片描述

the datatype of im <class 'numpy.ndarray'>
the shape of img:  (137, 203, 4)

可以看到,显示图像时自动加了坐标轴。


matplotlib库

import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片

im = mpimg.imread('E:/liuying/Pictures/test.png')
print("the datatype of im", type(im))  # 读取的数据类型
print("the shape of img: ", im.shape) # 数据的大小

plt.imshow(im) # 显示图片

结果如下:

the datatype of im <class 'numpy.ndarray'>
the shape of img:  (137, 203, 4)

在这里插入图片描述
如果不想看到坐标轴,可以采用以下方法去掉:

import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
%matplotlib inline  # 本来是想让python notebook显示图像的时候嵌入到显示框下,但好像不用也不影响,暂且先放着
im = mpimg.imread('E:/liuying/Pictures/test.png')
print("the datatype of im", type(im))  # 读取的数据类型
print("the shape of img: ", im.shape) # 数据的大小
plt.figure("image")
plt.imshow(im) # 显示图片
plt.axis('off') # 不显示坐标轴
plt.show()  # 不加好像也不影响结果

在这里插入图片描述

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值