【scikit-image】 图片读取、显示和保存

____tz_zs

skimage提供了io模块,顾名思义,这个模块是用来图片输入输出操作的。

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs
图片读取、显示与保存
skimage.io模块
"""
from skimage import io, color

# skimage提供了io模块,顾名思义,这个模块是用来图片输入输出操作的。
img = io.imread("./222.jpg")
print(img)
'''
[[[ 66  50  35]
  [ 67  49  37]
  [ 62  46  33]
  ..., 
  ..., 
  [ 56  33  17]
  [ 53  30  14]
  [ 52  29  13]]]
'''
print(img.shape)  # (333, 500, 3)
print(type(img))  # <class 'numpy.ndarray'>
io.imshow(img)
io.show()  # 图1

# as_grey 作为灰度图读取
img2 = io.imread("./222.jpg", as_grey=True)  # 同 gray = color.rgb2gray(img) 的结果
print(img2)
'''
[[ 0.20517059  0.20376392  0.1900498  ...,  0.2287349   0.22960588
   0.2237349 ]
 ..., 
 [ 0.20772039  0.26219686  0.28937294 ...,  0.14405451  0.1322898
   0.12836824]]
'''
print(img2.shape)  # (333, 500)
io.imshow(img2)
io.show()  # 图2

io.imsave('./222_save.jpg', img)
io.imsave('./222_save.png', img)
io.imsave('./222_save2.jpg',
          img2)  # UserWarning: Possible precision loss when converting from float64 to uint8.format(dtypeobj_in, dtypeobj_out))

# 再次读取刚刚保存的灰度图
im = io.imread('./222_save2.jpg')
print(im)
'''
[[54 53 48 ..., 59 59 58]
 [54 53 48 ..., 53 53 52]
 [54 53 48 ..., 56 55 54]
 ..., 
 [58 67 76 ..., 19 16 18]
 [56 65 74 ..., 32 30 30]
 [55 64 74 ..., 36 35 34]]
'''
print(im.shape)  # (333, 500)
io.imshow(im)
io.show()  # 图3

# 可以从 URL 路径读取图片
im_bd = io.imread('https://www.baidu.com/img/bd_logo1.png')
io.imshow(im_bd)
io.show()  # 图4
io.imsave('./ba_logo.png', im_bd)

·图1

·图2

·图3

·图4

·

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值