图像的读取、存储、类型转换

博客介绍了三种读取与显示图片的方法,包括matplotlib、cv2和PIL.Image。其中提到cv2.imshow()输入(R,G,B)通道时会出现图像色彩混乱的情况,最后还提及了最常见的三种图片格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、matplotlib读取与显示图片

2、用cv2读取与显示图片

3、PIL.Image读取图片

4、总结:


最常见的三种图片格式

str_bmp = r"../Data_set/00000_sr.bmp"
str_jpg = r"../Data_set/00205.jpg"
str_png = r"../Data_set/0901x2.png"

1、matplotlib读取与显示图片

import matplotlib.pyplot as plot
import matplotlib.image as pl_image
def matplot_dealwith(str):
    in_image = pl_image.imread(str)  # 类型numpy.array   PLT默认读取图片数据格式:(高,宽,通道(R,G,B))。
    plot.imshow(in_image)
    print(in_image.shape)   # (678, 1020, 3)
    plot.show()

2、用cv2读取与显示图片

def cv2_dealwith(str):
    img = cv2.imread(str)  # 打开图像,opencv默认读取图片的数据为: (高,宽,通道(B,G,R))。
    # img的类型: numpy.array
    print(img.shape)  # 图像通道顺序为:BGR注意0-0
    cv2.imshow("image", img)  # 显示图片,opencv默认读取图片的数据为: 注意0-0(高,宽,通道(B,G,R))。
    cv2.waitKey(0)

cv2.imshow()输入是(R,G,B)通道,则出现下面这种情况,图像色彩混乱

3、PIL.Image读取图片

from PIL import Image
import numpy as np
def pil_dealwith(str):
    in_image = Image.open(str)  # RGB(三通道)或者L(单通道灰度)
    print(type(in_image))  # 数据类型 <class 'PIL.PngImagePlugin.PngImageFile'>
    # in_image.save() #存图片
    np_img = np.array(in_image)   # pil 转numpy
    print(type(np_img.shape))
    print(np_img.shape)
    pil_img = Image.fromarray(np_img)   # numpy转 pil
    print(type(pil_img))
    in_image.show()

 

4、总结:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值