栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python读入图片的四种方式

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python读入图片的四种方式


title: python读入图片的四种方式
date: 2022-07-02 15:11:58
tags: image process python读入图片的四种方式
'''
多种读入图片并显示,保存的方法总结
'''

def read_image1(filename):
    '''
    python自带的读入图片的方法
    PIL
    '''
    from PIL import Image 
    import numpy as np
    
    image = Image.open(filename) #image 为PIL.Image.Image的类型
    image_array = np.array(image) #转为array形式

    image.show() #显示图片
    image.save('./PIL1.png') #保存图片

def read_image2(filename):
    '''
    opencv读入图片
    opencv
    '''
    import cv2
    
    image = cv2.imread(filename) #read image, 返回的就是array
    cv2.imshow('image', image) #显示图片
    key = cv2.waitKey(0) #等待任意键键入
    if key == ord('q') or key==27: #如果键入的是q或者esc(27为esc对应的ASCII码),则打印hello,
        print('hello')
    cv2.imwrite('cv2.png', image)


def read_image3(filename):
    '''
    matplotlib.image
    '''
    import matplotlib.image as mpimg
    import matplotlib.pyplot as plt

    image = mpimg.imread(filename) #读入图像

    plt.figure(num=1)
    plt.imshow(image)

    plt.savefig('plt.png') #保存图像
    plt.show() #显示图像

def read_image4(filenmae):
    '''
    skimage
    '''
    from skimage import io
    image = io.imread(filename)

    io.imshow(image)    
    io.imsave('skimage1.png', image)
    io.show()


if __name__ == '__main__':
    filename = '../3_axes.png'
    # read_image1(filename)
    # read_image2(filename)
    # read_image3(filename)
    read_image4(filename)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1001273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号