Uploading PIL image object to Amazon s3 python
You need to use a file-like object but you should not call getValue()
contrary to the accepted answer. Using the following snippet, you can then upload the image to S3 using in_mem_file
when calling upload_fileobj
:
from PIL import Image
import io
# Open image
pil_image = Image.open(response.raw)
# Save the image to an in-memory file
in_mem_file = io.BytesIO()
pil_image.save(in_mem_file, format=pil_image.format)
in_mem_file.seek(0)
# Upload image to s3
client_s3.upload_fileobj(
in_mem_file, # This is what i am trying to upload
AWS_BUCKET_NAME,
key,
ExtraArgs={
'ACL': 'public-read'
}
)
The .seek(0)
part is needed to rewind the file-like object if you see that the uploaded file is 0kB.
Instead of calling read() to get the file contents back, you 'save' the file to either a real file object or an file like object in memory. Then call getValue() on it.
Here is an example function you could pass file content into, print out the height and width, then return the file data back in a format that an AWS clients put_object function will accept as the Body parameter.
from PIL import Image
import io
def modify_image(image, format):
pil_image = Image.open(image)
# Prints out (1280, 960)
print(pil_image.size)
in_mem_file = io.BytesIO()
# format here would be something like "JPEG". See below link for more info.
pil_image.save(in_mem_file, format=format)
return in_mem_file.getvalue()
There are also separate width and height attributes here: http://pillow.readthedocs.io/en/3.4.x/reference/Image.html#attributes
See more about the file formats here http://pillow.readthedocs.io/en/3.4.x/handbook/image-file-formats.html
Note: Example uses Python 3.6.1