ValueError:图像大小为2000x100000像素,太大了。每个方向必须小于2^16

-1 投票
1 回答
69 浏览
提问于 2025-04-14 17:05

我正在尝试绘制5000张数据集中的图片(猫和狗的照片),用于图像分类。我使用的是Google Colab,即使在PyCharm中也出现了同样的错误。我在代码中使用了import matplotlib.pyplot as plt。所有图片的尺寸都是512x512。

#Calculate the number of rows and columns for subplots. Plot the images
num_images = len(df_BW)
num_rows = (num_images - 1) // 10 + 1
num_cols = min(num_images, 10)

fig, axes = plt.subplots(num_rows, 10, figsize=(20, num_rows * 2))
for idx, (img, label) in enumerate(zip(df_BW['image'], df_BW['label'])):
    ax = axes[idx // 10, idx % 10]
    ax.imshow(img, cmap = 'gray')
    ax.axis('off')
    ax.set_title(label)

#Hide empty subplots
for i in range(num_images, num_rows * 10):
    axes.flatten()[i].axis('off')

plt.tight_layout()
plt.show()

使用100张图片时,代码运行得很好(这是我测试过的最多的图片数量),但是当我尝试5000张图片时,它显示:

ValueError: 图片大小为2000x100000像素太大了。每个方向的尺寸必须小于2的16次方。

1 个回答

0

你在脚本里看到的这个10是硬编码的列数。如果你把它改成20,那么你就会生成一个4000 x 50000的图像,而不是2000 x 100000,这两个尺寸都在2的16次方的限制范围内。

顺便提一下,这说明你的图像其实是200 x 200像素,而不是512 x 512?

(另外,num_cols虽然定义了,但没有被使用,你可以把它删掉。)

撰写回答