____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
·