Jean-Pat
Jean-Pat

Reputation: 1871

How to show images respecting their relative size?

Images of different shapes stored in a list, are displayed as follow: enter image description here

plt.figure(figsize=(15,10))
H = ceil(sqrt(len(good_images)))
W = 1+len(good_images)//ceil(sqrt(len(good_images)))
print(len(good_images),H, W)
for i,im in enumerate(good_images):
        plt.subplot(H, W, i+1,xticks=[],yticks=[])
        plt.title(im.shape)
        plt.imshow(im, interpolation='nearest', cmap=plt.cm.gist_gray_r)

Is it possible to display the images and to conserve their relative size?

Upvotes: 1

Views: 230

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339560

You may share the axes such that all images live on the same scales.

import numpy as np
import matplotlib.pyplot as plt

N = 25
f = lambda n,m: np.random.rand(n,m)
g = lambda: f(np.random.randint(29,150), np.random.randint(29,150))
images = [g() for _ in range(N)]

fig, axs = plt.subplots(5,5, sharex=True, sharey=True)

for image, ax in zip(images, axs.flat):
    ax.imshow(image, cmap="Greys")
    ax.set_title(image.shape, fontsize=8)
    ax.autoscale()

fig.tight_layout()
plt.show()

enter image description here

Upvotes: 2

Related Questions