简体   繁体   中英

Uploading PIL image object to Amazon s3 python

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?

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

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.

You should use io.BufferIO

response = requests.get(url, stream= True)
f = io.BytesIO(response.content)
image = Image.open(f)

In case you are working with FileStorage from werkzeug.datastructures , having parsed an image using reqparse , then there is no need to convert the image to PIL.Image , you can use seek(0) on FileStorage itself.

Also do not forget to specify file's Content-type .

parser = reqparse.RequestParser()
parser.add_argument('image', help='image cannot be blank', type=FileStorage, 
location='files', required=True)

args = parser.parse_args()
image = args['image']

image.seek(0)
s3_client.upload_fileobj(image, self.BUCKET_NAME, filename, ExtraArgs={'ContentType': 'image/jpeg'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM