'Read images from url and put them on a dataframe column with the label to another column in python
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)
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ze'ev Ben-Tsvi |
