1

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

1
  • You pass some raw PNG bytes to frombuffer(). Did you maybe want to have a PNG decoder turn them into pixels first? Commented Apr 3, 2018 at 0:47

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.