是否需要将熊猫数据框转换为numpy数组以显示图像和存储在其中的坐标?

问题描述 投票:0回答:1

我有一个熊猫数据框,其中存储了两个图像网址及其注释详细信息(x,y坐标,高度和宽度)。我想以python显示图像。我应该首先将数据帧转换为numpy数组(以最终使用matplotlib imshow来显示图像)。还是可以使用matplotlib从数据框中提取数据以显示图像?哪个更容易?

python dataframe matplotlib numpy-ndarray
1个回答
1
投票
import pandas as pd
import requests

df = pd.DataFrame({'image_url':['https://dcist.com/wp-content/uploads/sites/3/2020/02/wilford_newsletter.jpg'], 'about':['a cat']})

# Use df.iloc[0] to pull the first image url, and requests to download the data for the image
a = plt.imread(requests.get(df.iloc[0]['image_url'], stream=True).raw, format='jpeg')

plt.imshow(a)
plt.show()

Image from first url in dataframe shown in matplotlib plot

© www.soinside.com 2019 - 2024. All rights reserved.