I want to get an image from the web and upload it to Amazon s3. While doing so, I would like to check the image dimensions. I have the following code in Python 3:
from PIL import Image
import requests
# Get response
response = requests.get(url, stream= True)
# Open image
im = Image.open(response.raw)
# Get size
size = im.size
# Upload image to s3
S3.Client.upload_fileobj(
im, # This is what i am trying to upload
AWS_BUCKET_NAME,
key,
ExtraArgs={
'ACL': 'public-read'
}
)
The problem is that the PIL image object does not support read. I get the following error when i try to upload the PIL Image object im
.
ValueError: Fileobj must implement read
It works when I just try to upload the 'response.raw', but I need to get the image dimensions. How can I change the PIL image object to a file-like-object? Is there an easier way to get the dimensions while still being able to upload the image to s3?
So the question is; how do I upload an image to s3 after getting the dimensions of an image?