Harry Moreno
Harry Moreno

Reputation: 11673

Plot a grid of images retrieved via url in Python

How does one display a grid of images using Matplotlib and images retrieved over the network?

I tried the following

import matplotlib.pyplot as plt
import numpy as np
import urllib.request

a_url = 'https://via.placeholder.com/255x255'

# fetch image from placeholder.com
data = urllib.request.urlopen(a_url).read()
np_arr = np.frombuffer(data)

plt.plot(np_arr)

'''
images = [data]
print(type(data))

plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
    plt.subplot(len(images) / columns + 1, columns, i + 1)
    plt.imshow(image)
'''

but get the error buffer size must be a multiple of element size

Upvotes: 1

Views: 2610

Answers (2)

Harry Moreno
Harry Moreno

Reputation: 11673

solution

import matplotlib.pyplot as plt

a_url = 'https://via.placeholder.com/255x255'

data = plt.imread(a_url)
images = [data for _ in range(13)]

plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
    plt.subplot(len(images) / columns + 1, columns, i + 1)
    plt.imshow(image)

interactive link https://drive.google.com/file/d/1a-toRZ9rOL-_BwBuD1kYdAgZnVj25C4v/view?usp=sharing

Upvotes: 3

Wei Zhang
Wei Zhang

Reputation: 334

In fact, you can directly retrieve images with plt.imreadand a url, and then show it with plt.imshow.

Upvotes: 0

Related Questions