PIL Image from Url

Had some troubles finding what seemed to be such an easy Google search. Needed the capability to load an image based on a url. Here is my solution:

import urllib2 as urllib			
from PIL import Image
from cStringIO import StringIO
			
img_file = urllib.urlopen(self.image.url)
im = StringIO(img_file.read())
resized_image = Image.open(im)

2 Responses to PIL Image from Url

  1. Paul Kenjora says:

    Nice,

    Worked like a charm! I also discovered the difference between:

    import StringIO vs. from cStringIO import StringIO

    I was actually grabbing an image from S3 and was about to invoke the S3 library “get” when I saw this post. Right of course, my S3 images are public so why not just use urllib2.

    Thanks for making me think outside the box.

    Regards

  2. Tom says:

    Great, thanks for sharing this.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>