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)


Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐