Reputation: 732
I have a list of image locations
image_locations = [path/car.jpg, path/tree.jpg, ..., path/apple.jpg]
Each image has a different size and shape.
What is the best way to display/print these images (ideally in some grid) in Jupyter notebook?
Upvotes: 0
Views: 2935
Reputation: 113
You can use the modules pyplot
, image
and then the function subplots()
:
# Import modules
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Read images
img1 = mpimg.imread('myImage1.png')
img2 = mpimg.imread('myImage2.png')
img3 = mpimg.imread('myImage3.png')
img4 = mpimg.imread('myImage4.png')
# Set your canvas (fig) and the number of axes (images) you want to display
# In this example I want to display my 4 images in a grid 2x2
fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(nrows=2,ncols=2,figsize=(15,10))
ax1.imshow(img1)
ax1.set_title("Image 1")
ax2.imshow(img2)
ax2.set_title("Image 2")
ax3.imshow(img3)
ax3.set_title("Image 3")
ax4.imshow(img4)
ax4.set_title("Image 4")
plt.show()
# The same example with just the first two images in a grid 1x2
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=(15,10))
ax1.imshow(img1)
ax1.set_title("Image 1")
ax2.imshow(img2)
ax2.set_title("Image 2")
plt.show()
Fig
is just a container of your axes
.
Upvotes: 3