从URL读取图像,然后将它们放在带有标签的数据框列上到Python的另一列

发布于 2025-01-23 04:57:59 字数 921 浏览 1 评论 0原文

我想读取来自URL的图像,并将其放在Pandas DataFrame列中,然后将标签放在另一列。我该怎么做,请帮忙。以下是我阅读的文件中的数据框。有4个标记的URL页面。但是我必须从中排除.svg图像,因为它没有显示任何图片。
因此最终结果将继续 df = {'image':....,'label':['ICON','iCON','iCON']}} 谢谢

import pandas as pd

data={'image_url':['https://www.philips.com/c-dam/b2c/en_US/experience/sound-and- 
vision/headphones/fidelio-l3/L3_38_hr_88x88.png'
,'https://www.philips.com/c-dam/b2c/tv/categorypage/us/AndroidTVLP/HDR.png'
,'https://www.philips.com/c-dam/b2c/category-pages/sound-and- 
vision/headphones/master/homepage/awaraness-mode-anc.png'],
,'https://https://www.philips.com/c-dam/b2c/master/experience/pe/shavers/360-d.svg'
'label':['icon','icon','icon','icon']}

df=pd.DataFrame(data)

#Below is what I tried so far with no success 
img=[]
image=df['image_url']
for i in image:
if not i.endswith('.svg'):
response = requests.get(i)
img1 = Image.open(BytesIO(response.content))
img.extend(img1)

I would like to read images from url and place them on a pandas dataframe column and place label to another another column. How can I do that please help. Below is the dataframe from the file I read. There 4 url pages with the 4 labeled.But I have to exclude .svg images from it since it does not display any pictures.
So final outcome will be on
df={'image':....,'label':['icon','icon','icon']}
Thank you

import pandas as pd

data={'image_url':['https://www.philips.com/c-dam/b2c/en_US/experience/sound-and- 
vision/headphones/fidelio-l3/L3_38_hr_88x88.png'
,'https://www.philips.com/c-dam/b2c/tv/categorypage/us/AndroidTVLP/HDR.png'
,'https://www.philips.com/c-dam/b2c/category-pages/sound-and- 
vision/headphones/master/homepage/awaraness-mode-anc.png'],
,'https://https://www.philips.com/c-dam/b2c/master/experience/pe/shavers/360-d.svg'
'label':['icon','icon','icon','icon']}

df=pd.DataFrame(data)

#Below is what I tried so far with no success 
img=[]
image=df['image_url']
for i in image:
if not i.endswith('.svg'):
response = requests.get(i)
img1 = Image.open(BytesIO(response.content))
img.extend(img1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2025-01-30 04:57:59

这是您想要的吗?

img = []
image = df['image_url']
for i in image:
    if not i.endswith('.svg'):
        response = requests.get(i)
        img1 = Image.open(BytesIO(response.content))
        img.extend([img1])
    else:
        img.extend([None])
df['image'] = pd.DataFrame(img)

Is this what you were looking for?

img = []
image = df['image_url']
for i in image:
    if not i.endswith('.svg'):
        response = requests.get(i)
        img1 = Image.open(BytesIO(response.content))
        img.extend([img1])
    else:
        img.extend([None])
df['image'] = pd.DataFrame(img)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文