You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python三大图像库读取同一图像为何输出尺寸不同?

Why do imageio, PIL.Image, and cv2 return different dimensions when reading the same image?

Great question! This is a super common gotcha when working with image libraries in Python—let's break down exactly why each library gives you different results, using your example:

1. Axis Order: Width vs. Height First

The biggest immediate difference is how each library reports the image's dimensions:

  • PIL.Image: Follows traditional image coordinate conventions, where the first value is the width (horizontal axis, x) and the second is the height (vertical axis, y). That's why your b.size returns (334, 500)—334 is the width, 500 is the height. Note that PIL returns an Image object, not a numpy array, so its size property uses this (W, H) order.
  • imageio and cv2: Both return numpy arrays, which use matrix-style indexing: the first dimension is rows (corresponding to image height) and the second is columns (corresponding to image width). So their shape reports (500, 334, ...)—500 is height, 334 is width.

If you convert the PIL Image object to a numpy array, you'll see it matches the shape from imageio/cv2 (minus channel differences):

import numpy as np
from PIL import Image
b = Image.open('test_img.png')
print(np.array(b).shape)  # Output: (500, 334, 4) (for your RGBA image)

2. Channel Count & Handling

The second difference comes down to how each library handles transparency (Alpha channels) in PNGs:

  • imageio.imread: By default, it reads all channels present in the image. Your test_img.png has an Alpha channel (transparency), so it returns a 4-channel RGBA array ((500, 334, 4)), with dtype uint8 (standard for 0-255 pixel values).
  • cv2.imread: By default, it ignores Alpha channels and only reads the first 3 color channels—but note that OpenCV uses BGR order instead of RGB! That's why you get (500, 334, 3). If you want to include the Alpha channel, you need to use the flag cv2.IMREAD_UNCHANGED:
    import cv2
    c = cv2.imread('test_img.png', cv2.IMREAD_UNCHANGED)
    print(c.shape)  # Output: (500, 334, 4)
    
  • PIL.Image.open: The Image object's mode will reflect the channel count (e.g., 'RGBA' for 4 channels). When converted to a numpy array, it will have the same channel count as imageio, just starting with (W, H) in the size property.

Quick Recap

  • imageio.imread: Returns numpy array in (Height, Width, Channels) order, reads all channels including Alpha
  • PIL.Image.open: Returns Image object with (Width, Height) size, preserves original channel mode
  • cv2.imread: Returns numpy array in (Height, Width, Channels) order, ignores Alpha by default and uses BGR channel order

内容的提问来源于stack exchange,提问作者PURNENDU MISHRA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:28:57