Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple PIL images into one single plot

I have a n amount of images with a format:

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=64x64 at 0x7F555F6E3898>

all together in a list. I can visualize each individually but I want to visualize them all together, one beside another until it gets too long and then go to the next row (for instance, n/4 rows with 4 images in each row). Once this is done, I would like to save it as a jgp as well.

I tried using subplot from matplotlib but it says that the values are unhashable.

My code is:

from os import listdir
from os.path import isfile, join

files = [f for f in listdir(mypath) if isfile(join(mypath, f))]    
files.sort()
files.sort(key = len)
im = []
for jpg in files:
    im.append(Image.open(mypath+'/'+jpg))

for i in im:
    image = np.asarray(i)
    plt.subplot(3,np.floor(len(image)/3),image)
plt.show()
like image 897
Antonio López Ruiz Avatar asked Nov 24 '25 07:11

Antonio López Ruiz


1 Answers

update your usage for subplot, then it should be good. please see below sample code.

import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

# get sample data
from sklearn import datasets
x,y=datasets.load_digits(n_class=10, return_X_y=True)
x = x[0:100,:]
x = np.reshape(x,[100,8,8])

# plot
for i in range(x.shape[0]):
    if i >= 9:
        break
    image = x[i,:].squeeze()
    plt.subplot(3,3,i+1)
    plt.imshow(image,cmap='gray',interpolation='none')

enter image description here

alternatively:

# https://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html
from mpl_toolkits.axes_grid1 import ImageGrid
fig = plt.figure(1,(10,10))
grid = ImageGrid(fig, 111,
                 nrows_ncols=(2,7),
                 axes_pad=0.1,
                 )
for i in range(14):
    image = x[i,:].squeeze()
    grid[i].imshow(image,cmap='gray',interpolation='none')

enter image description here

like image 175
pangyuteng Avatar answered Nov 26 '25 23:11

pangyuteng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!