二、skimage图像的读取、显示与保存

一、读取、显示、保存

读取单张彩色rgb图片,使用skimage.io.imread(fname)函数,带一个参数,表示需要读取的文件路径。显示图片使用skimage.io.imshow(arr)函数,带一个参数,表示需要显示的arr数组(读取的图片以numpy数组形式计算)。

读取单张灰度图片,使用skimage.io.imread(fname,as_grey=True)函数,第一个参数为图片路径,第二个参数为as_grey, bool型值,默认为False

skimage程序自带了一些示例图片,如果我们不想从外部读取图片,就可以直接使用这些示例图片(有些图片被移除了):

astronaut

宇航员图片

coffee

一杯咖啡图片

lena

lena美女图片

camera

拿相机的人图片

coins

硬币图片

moon

月亮图片

checkerboard

棋盘图片

horse

马图片

page

书页图片

chelsea

小猫图片

hubble_deep_field

星空图片

text

文字图片

clock

 时钟图片

immunohistochemistry

结肠图片

 

 
# -*- coding: utf-8 -*-
# @Time : 2019/12/9 23:56
# @Author : sxp
# @Email : 
# @File : read_show_save.py
# @Project : python_common

from skimage import io,data,data_dir

def read_rgb_or_gray(path=None,is_self_img=True,type='rgb'):
    img = None
    if is_self_img:
        if type == 'rgb':
            img = io.imread(path)#读取单张彩色rgb图片
        if type == 'gray':
            img = io.imread(path,as_gray=True)#读取单张灰度图片
    else:
        print('data_dir = {}'.format(data_dir))#图片存放在skimage的安装目录下面,路径名称为data_dir,
        img = data.camera()#程序自带图片
    return img

def show_img(img):
    io.imshow(img)
    io.show()

def save_img(path,img):
    io.imsave(path,img)#也起到了转换格式的作用

if __name__ == '__main__':
    path = r"E:\PythonProjects\python_common\skimage\images\lena.jpg"
    save_path = r'E:\PythonProjects\python_common\skimage\io\\save.jpg'
    img = read_rgb_or_gray(path,is_self_img=True,type='rgb')

    show_img(img)
    save_img(save_path,img)

二、查看图片属性

# -*- coding: utf-8 -*-
# @Time : 2019/12/9 23:56
# @Author : sxp
# @Email : 
# @File : read_show_save.py
# @Project : python_common

from skimage import io,data,data_dir

def read_rgb_or_gray(path=None,is_self_img=True,type='rgb'):
    img = None
    if is_self_img:
        if type == 'rgb':
            img = io.imread(path)#读取单张彩色rgb图片
        if type == 'gray':
            img = io.imread(path,as_gray=True)#读取单张灰度图片
    else:
        print('data_dir = {}'.format(data_dir))#图片存放在skimage的安装目录下面,路径名称为data_dir,
        img = data.camera()#程序自带图片
    return img


if __name__ == '__main__':
    path = r"E:\PythonProjects\python_common\skimage\images\lena.jpg"
    save_path = r'E:\PythonProjects\python_common\skimage\io\\save.jpg'
    img = read_rgb_or_gray(path,is_self_img=True,type='rgb')
    print(type(img))  # 显示类型
    print('img.shape = {}'.format(img.shape))  # 显示尺寸
    print('图片宽度 = {}'.format(img.shape[0]))  #
    print('图片高度 = {}'.format(img.shape[1]))  #
    print('图片通道数 = {}'.format(img.shape[2]))  #
    print('显示总像素个数 = {}'.format(img.size))  #
    print('最大像素值 = {}'.format(img.max()))  #
    print('最小像素值 = {}'.format(img.min()))  #
    print('像素平均值 = {}'.format(img.mean()))  #

猜你喜欢

转载自blog.csdn.net/s294878304/article/details/103467794
今日推荐