I'm trying to store as a PIL object in a new column of a dataframe pictures that are located in a column of the same dataframe in the form of URL's.
I've tried the following code:
import pandas as pd
from PIL import Image
import requests
from io import BytesIO
pictures = [None] * 2
df = pd.DataFrame({'project_id':["1", "2"],
'image_url':['http://www.personal.psu.edu/dqc5255/gl-29.jpg',
'https://www.iprotego.com/wp-content/uploads/google.jpg']})
# Previously the second link was broken and led to an error, I just edited it and now works fine
df.insert(2, "pictures", pictures, True)
for i in range(2):
r = requests.get(df.iloc[i,1])
df.iloc[i,2] = Image.open(BytesIO(r.content))
df
I expected to get a dataframe with this format but including both training examples:
project_id image_url pictures
0 1 http://www.personal.psu.edu/dqc5255/gl-29.jpg <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=400x300 at 0x116EF9AC8>
But instead got the following error:
OSError: cannot identify image file <_io.BytesIO object at 0x116ec2f10>