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)


  • 9
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值