Matplotlib imread() 方法

Matplotlib imread() 方法完全指南

imread() 是 Matplotlib 中用于 读取图像文件为 NumPy 数组 的核心函数,广泛用于 图像处理、深度学习、遥感分析、医学影像 等场景。本教程带你从入门到 专业级,掌握 读取格式、色彩空间、透明通道、批量读取、坐标信息、与 imshow 配合、图像预处理、性能优化 等全部技巧。


一、imread() vs PIL.Image.open() 对比

方法优势适用场景
plt.imread()无缝集成 Matplotlib,支持 imshow可视化、快速原型
PIL.Image.open()更强大,更多格式图像处理、深度学习

本教程专注 plt.imread()可视化与科学计算 中的最佳实践。


二、基本语法

import matplotlib.pyplot as plt
import numpy as np

# 读取图像
img = plt.imread('image.png')

print(f"形状: {img.shape}")     # (H, W, C) 或 (H, W)
print(f"数据类型: {img.dtype}") # float32 或 uint8
print(f"取值范围: {img.min()} ~ {img.max()}")

三、支持的文件格式

格式扩展名说明
PNG.png推荐,无损,透明
JPG/JPEG.jpg, .jpeg有损,小文件
BMP.bmp无压缩
TIFF.tif, .tiff科学数据,多通道
GIF.gif动图(只读第一帧)
img_png = plt.imread('data.png')   # float32, [0,1]
img_jpg = plt.imread('data.jpg')   # float32, [0,1]

Matplotlib 自动将 uint8float32 并归一化到 [0,1]


四、色彩空间与通道

1. 灰度图(2D)

gray = plt.imread('gray.png')      # shape: (H, W)
plt.imshow(gray, cmap='gray')
plt.colorbar()
plt.show()

2. RGB 图(3 通道)

rgb = plt.imread('rgb.png')        # shape: (H, W, 3)
print(rgb.shape)  # e.g., (512, 512, 3)

3. RGBA 图(4 通道,透明)

rgba = plt.imread('alpha.png')     # shape: (H, W, 4)
alpha = rgba[:, :, 3]              # 透明度通道

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(rgba)
ax1.set_title('带透明度')

ax2.imshow(alpha, cmap='gray')
ax2.set_title('透明度通道')
plt.show()

五、与 imshow() 完美配合

img = plt.imread('stinkbug.png')

plt.figure(figsize=(8, 6))
plt.imshow(img)
plt.title('Matplotlib 经典臭虫图')
plt.axis('off')  # 隐藏坐标轴
plt.show()

六、批量读取图像

import os
from pathlib import Path

folder = 'images/'
image_paths = list(Path(folder).glob('*.png'))

images = []
for path in image_paths:
    img = plt.imread(path)
    images.append(img)

print(f"读取了 {len(images)} 张图像")
print(f"第一张形状: {images[0].shape}")

七、读取网络图像

import requests
from io import BytesIO

url = 'https://matplotlib.org/stable/_images/stinkbug.png'
response = requests.get(url)
img = plt.imread(BytesIO(response.content), format='png')

plt.imshow(img)
plt.axis('off')
plt.show()

八、图像预处理(常见操作)

img = plt.imread('photo.jpg')

# 1. 裁剪
cropped = img[100:400, 200:500]

# 2. 缩放(插值)
from scipy.ndimage import zoom
resized = zoom(img, (0.5, 0.5, 1))  # 宽高减半

# 3. 归一化(uint8 → [0,1])
img_norm = img.astype(np.float32) / 255.0

# 4. 转为灰度
gray = np.mean(img, axis=2)

# 5. 直方图均衡化(增强对比度)
from skimage import exposure
gray_eq = exposure.equalize_hist(gray)

九、读取 TIFF 科学图像(多页/元数据)

# 注意:Matplotlib 对多页 TIFF 支持有限
# 推荐使用:rasterio, tifffile, PIL

try:
    img_tiff = plt.imread('multipage.tif')  # 只读第一页
    print("TIFF 第一页:", img_tiff.shape)
except Exception as e:
    print("读取失败,建议用 tifffile:")
    # !pip install tifffile
    # import tifffile
    # img = tifffile.imread('multipage.tif')

十、完整专业示例(遥感图像分析)

import matplotlib.pyplot as plt
import numpy as np
import requests
from io import BytesIO

plt.style.use('seaborn-v0_8')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 从网络读取 Landsat 假彩色图像(模拟)
url = 'https://upload.wikimedia.org/wikipedia/commons/5/53/Landsat_8_image_of_London.jpg'
response = requests.get(url)
img = plt.imread(BytesIO(response.content))

# 假彩色合成(假设 B4=红, B3=绿, B2=蓝)
# 实际需多光谱数据,此处模拟
R = img[:, :, 0] * 1.5
G = img[:, :, 1] * 1.2
B = img[:, :, 2] * 0.8

# 归一化
R = np.clip(R, 0, 1)
G = np.clip(G, 0, 1)
B = np.clip(B, 0, 1)
false_color = np.stack([R, G, B], axis=2)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))

# 原图
ax1.imshow(img)
ax1.set_title('真彩色图像', fontsize=14, fontweight='bold')
ax1.axis('off')

# 假彩色
ax2.imshow(false_color)
ax2.set_title('假彩色合成(植被增强)', fontsize=14, fontweight='bold')
ax2.axis('off')

# 添加城市标注
cities = {'伦敦': (0.45, 0.55), '格林威治': (0.58, 0.48)}
for city, (x, y) in cities.items():
    ax2.plot(x*img.shape[1], y*img.shape[0], 'r*', markersize=12, markeredgecolor='black')
    ax2.text(x*img.shape[1], y*img.shape[0]-20, city, 
             color='red', fontweight='bold', fontsize=11,
             bbox=dict(boxstyle="round", facecolor='white', alpha=0.8))

plt.suptitle('Matplotlib imread() 遥感图像分析示例', fontsize=18, fontweight='bold', y=0.95)
plt.tight_layout()
plt.savefig('landsat_analysis.png', dpi=300, bbox_inches='tight')
plt.show()

十一、imread() 速查表(收藏用)

# 基础读取
img = plt.imread('image.png')

# 网络图像
img = plt.imread(BytesIO(requests.get(url).content))

# 批量读取
images = [plt.imread(p) for p in Path('folder').glob('*.jpg')]

# 转为 uint8
img_uint8 = (img * 255).astype(np.uint8)

# 提取通道
R, G, B = img[:, :, 0], img[:, :, 1], img[:, :, 2]

# 灰度转换
gray = np.mean(img, axis=2)

# 裁剪
cropped = img[100:300, 200:400]

# 缩放
from scipy.ndimage import zoom
resized = zoom(img, (0.5, 0.5, 1))

十二、常见问题解决

问题解决方案
图像颜色偏暗plt.imread() 自动归一化,检查原图
读取失败检查路径、格式、权限
内存爆炸分块读取或用 PIL + np.array()
TIFF 多页改用 tifffile.imread()
坐标轴显示plt.imshow(img); plt.axis('off')

十三、性能优化建议

# 1. 避免重复读取
cache = {}
def read_cached(path):
    if path not in cache:
        cache[path] = plt.imread(path)
    return cache[path]

# 2. 读取缩略图(大图预览)
from PIL import Image
def read_thumbnail(path, size=(256,256)):
    with Image.open(path) as im:
        im.thumbnail(size)
        return np.array(im) / 255.0

十四、与深度学习框架配合

import torch
from torchvision import transforms

# Matplotlib → PyTorch Tensor
img = plt.imread('image.png')           # (H, W, 3), float32 [0,1]
tensor = torch.from_numpy(img).permute(2, 0, 1)  # (C, H, W)

# 标准化
transform = transforms.Compose([
    transforms.ToTensor(),  # 自动 [0,1] + (C,H,W)
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
])

官方文档

  • imread API:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imread.html
  • 支持格式:https://matplotlib.org/stable/users/explain/figure/backends.html

总结:三步使用 imread

# 1. 读取
img = plt.imread('data.png')  # float32, [0,1]

# 2. 处理(裁剪、转换、增强)
cropped = img[100:400, 200:500]
gray = np.mean(cropped, axis=2)

# 3. 显示或保存
plt.imsave('output.png', gray, cmap='gray')
plt.imshow(gray, cmap='gray'); plt.show()

一键读取并显示

img = plt.imread('https://matplotlib.org/stable/_images/stinkbug.png')
plt.imshow(img); plt.axis('off'); plt.title('臭虫图'); plt.show()

需要我为你:

  • 生成 批量图像处理脚本(缩放 + 水印)?
  • 制作 遥感多光谱合成工具
  • 输出 医学影像 DICOM 读取方案
  • 提供 与 OpenCV/PIL 对比表格

告诉我你的需求!

文章已创建 2481

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部