ValueError: Image size of 2000x100000 pixels is too large. It must be less than 2^16 in each direction

71 views Asked by At

I am trying to plot 5000 images of dataset (pictures of cats and dogs) for an image classification. I am using google colab and even when I tried using pycharm it also shows the same error. I am using import matplotlib.pyplot as plt for my code. All of the images' dimensions are 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()

The code works fine using 100 images (the highest images i have test) but when I tried 5000 image it says:

ValueError: Image size of 2000x100000 pixels is too large. It must be less than 2^16 in each direction.

1

There are 1 answers

1
Darren Cook On

The 10 running through your script is hard-coding the number of columns. If you instead changed it to 20 then instead of 2000 x 100000 you'd make an image 4000 x 50000, which are both inside the 2^16 limit.

By the way, that suggests your images are actually 200 x 200 pixels, not 512x512?

(As another by the way, num_cols is defined but not used; you could delete it.)