简体   繁体   中英

Trying to display images with PIL and BytesIO but returns nothing

I'm trying to display the logos from a dataset. The dataset looks like this:

Player      Club Logo        
tom         https://abc.png
jerry       https://def.png
peter       https://frf.png
woody       https://awt.png

However, it didnt return me any logos. All it did show was 4 empty grid boxes. My code is below. I also did try to use im = Image.open(BytesIO(r.content)).show() but the logos ended up opening on my computer instead.

import matplotlib.pyplot as plt
import requests

from PIL import Image
from io import BytesIO

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

for i in range(4):
    r = requests.get(df['Club Logo'][i])
    im = Image.open(BytesIO(r.content))

plt.show()

Thanks

Starting with these images:

"0.png" :

在此处输入图片说明

"1.png" :

在此处输入图片说明

"2.png" :

在此处输入图片说明

"3.png" :

在此处输入图片说明

I think you want this:

#!/usr/bin/env python3

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

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

for i in range(4): 
    # Load image and make into Numpy array
    im = Image.open(f'{i}.png').convert('RGB') 
    na = np.array(im) 
    # Shove into the plot
    ax[i%2][i//2].imshow(na) 

fig.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM