Can't open image retrieved from S3 with PIL

966 views
Skip to first unread message

rootsmith

unread,
Jul 2, 2010, 12:30:24 AM7/2/10
to boto-users
I know this may be more of a question for PIL people but I'm thinking
*somebody* must be doing this exact same scenario with boto.

If I retrieve an image from boto 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) # PIL
# .. here I wish to crop the image before spitting it back to the
calling environment

I know that I can successfully retrieve the file because if 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 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?

Chris Moyer

unread,
Jul 2, 2010, 10:36:23 AM7/2/10
to boto-...@googlegroups.com
What happens if you use a TemporaryFile instead of a StringIO, for
that matter what happens if you actually download the file to a local
filesystem before sending it off to PIL?

> --
> You received this message because you are subscribed to the Google Groups "boto-users" group.
> To post to this group, send email to boto-...@googlegroups.com.
> To unsubscribe from this group, send email to boto-users+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/boto-users?hl=en.
>
>

--
Chris Moyer

Kevin J. Smith

unread,
Jul 2, 2010, 11:38:39 AM7/2/10
to boto-...@googlegroups.com
Same result if I use TemporaryFile, however, everything works just
fine if I download the file to disk first and then open with PIL. But
I am trying to avoid the disk write then read again for performance.
Again, from the evidence there doesn't appear to be anything wrong
with the file.

KJS

Matt Billenstein

unread,
Jul 2, 2010, 11:43:49 AM7/2/10
to boto-...@googlegroups.com
Dump the contents of the StringIO to a file and diff to the actual image
-- either you're getting the correct data or it's getting garbled
somewhere...

m

> --
> You received this message because you are subscribed to the Google Groups "boto-users" group.
> To post to this group, send email to boto-...@googlegroups.com.
> To unsubscribe from this group, send email to boto-users+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/boto-users?hl=en.
>

--
Matt Billenstein
ma...@vazor.com
http://www.vazor.com/

Victor Trac

unread,
Jul 2, 2010, 11:47:28 AM7/2/10
to boto-...@googlegroups.com
What happens if, using TemporaryFile, you do:

...
key.get_file(file)
file.seek(0)
Image.open(file) 
...

-- Victor

Kevin J. Smith

unread,
Jul 2, 2010, 2:35:48 PM7/2/10
to boto-...@googlegroups.com
AH!  That's it!  The operation of key.get_file(file) must end at the end of the file after writing in the contents (...makes sense...) so I need to seek out the beginning of the file before passing it to PIL.

I am still using StringIO btw so it is not necessary to go to a TemporaryFile to get it to work.

Cheers all, thanks for the help!
--
Never take life seriously. Nobody gets out alive anyway.
Reply all
Reply to author
Forward
0 new messages