2

I have some jpegs in a directory. I want to display them in a window in rows and columns. For example if I have 10 pictures, I want to display them a 2 rows by 5 columns table.
There is a subplot(m, n, k) command in MATLAB and Octave. How can I do similar thing in python?

I have tried pillow with PIL.Image and show() method but it is very limited and displays only 1 image.

1- How to do this natively (not in browser)?
2- How to do this using matplotlib?
3- How to do this in browser using Jupyter?

Zeta.Investigator
  • 911
  • 2
  • 14
  • 31
  • @DizietAsahi think he wants the same functionality in matplotlib. – Andrei Oct 01 '18 at 14:08
  • See https://matplotlib.org/users/image_tutorial.html and https://matplotlib.org/gallery/subplots_axes_and_figures/subplots_demo.html – Diziet Asahi Oct 01 '18 at 14:12
  • @Andrei you're right, I did not look closely at the post I was linking, I assumed it was matplotlib code – Diziet Asahi Oct 01 '18 at 14:13
  • @DizietAsahi Can matplotlib display local **jpeg** also? – Zeta.Investigator Oct 01 '18 at 14:15
  • hmm... from docs: Loading image data is supported by the Pillow library. Natively, matplotlib only supports PNG images. The commands shown below fall back on Pillow if the native read fails. – Zeta.Investigator Oct 01 '18 at 14:16
  • 1
    Those are really three questions and each has its own answer. (1) concatenate the images with Pillow. (2) use subplots in matplotlib and imshow the images (3) Create a HTML table with the images in it. Those are orthorgonal solutions and while I might help with any of them, writing up a complete answer with code for all three of them is above my current bandwidth. Plus, each of those is probably been answered already somewhere. – ImportanceOfBeingErnest Oct 01 '18 at 14:20
  • @ImportanceOfBeingErnest Thanks. (1) I knew you could combine multiple images and then show them but I was wondering if there is a specific method for that. (3) By browser, I meant more like Jypter notebook – Zeta.Investigator Oct 01 '18 at 14:26
  • I closed as duplicate of 3 existing questions, one for each. There might still be problems you face for your particular case, in which case please ask a very specific question about the actual problem. If you must, ask 3 specific questions. – ImportanceOfBeingErnest Oct 01 '18 at 14:32

1 Answers1

9
import matplotlib.pyplot as plt
from PIL import Image

fig,ax = plt.subplots(2,5)

filenames=['\path\to\img\img_{}.jpg'.format(i) for i in range(10)] #or glob or any other way to describe filenames
for i in range(10):
    with open(filenames[i],'rb') as f:
        image=Image.open(f)
        ax[i%2][i//2].imshow(image)
fig.show()
Andrei
  • 471
  • 6
  • 10