Can't open image retrieved from S3 with PIL

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't open image retrieved from S3 with PIL

Kevin J. Smith-2
Hi,

I know this may have to do with my use of S3 but I'm searching for any little thread that I can hang onto to solve this problem.

If I retrieve an image from boto (S3) and then try and open it with PIL I get an IOError: cannot identify image file.

Here is the code:

s3 = S3Connection('blah','blah')
bucket = s3.get_bucket('my_bucket')
key = bucket.get_key(key_name)
file = StringIO()
key.get_file(file)
Image.open(file)
# .. here I wish to crop the image before spitting it back to the calling environment

I know that the file I retrieve with the "key.get_file(file)" statement is in good shape because if I take out the PIL code and turn around and set the Content-Type header to image/jpeg and fire the file object back at the browser (this code is in a web app controller) the image appears just fine!

By briefly looking at the code in PIL it seems it is inspecting the first 16 bytes of the file and determining if it can recognize it but I don't understand why it isn't recognizing it.

Any suggestions?

Cheers

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Can't open image retrieved from S3 with PIL

Oliver Tonnhofer-4

On 02.07.2010, at 06:44, Kevin J. Smith wrote:
> Here is the code:
>
> s3 = S3Connection('blah','blah')
> bucket = s3.get_bucket('my_bucket')
> key = bucket.get_key(key_name)
> file = StringIO()
> key.get_file(file)
> Image.open(file)
> # .. here I wish to crop the image before spitting it back to the calling environment

Do you get any error? You could try to `file.seek(0)` before opening it with PIL.

Regards,
Oliver

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Can't open image retrieved from S3 with PIL

Kevin J. Smith-2
Thanks, Oliver.  That was the trick.

On 5 July 2010 22:46, Oliver Tonnhofer <[hidden email]> wrote:

On 02.07.2010, at 06:44, Kevin J. Smith wrote:
> Here is the code:
>
> s3 = S3Connection('blah','blah')
> bucket = s3.get_bucket('my_bucket')
> key = bucket.get_key(key_name)
> file = StringIO()
> key.get_file(file)
> Image.open(file)
> # .. here I wish to crop the image before spitting it back to the calling environment

Do you get any error? You could try to `file.seek(0)` before opening it with PIL.

Regards,
Oliver




--
Never take life seriously. Nobody gets out alive anyway.

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig