title: Python/Django download Image from URL, modify, and save to ImageField
body: <p>I've been looking for a way to download an image from a URL, preform some image manipulations (resize) actions on it, and then save it to a django ImageField.  Using the two great posts (linked below), I have been able to download and save an image to an ImageField.  However, I've been having some trouble manipulating the file once I have it. </p>
  
  <p>Specifically, the model field save() method requires a File() object as the second parameter.  So my data has to eventually be a File() object.  The blog posts linked below show how to use urllib2 to save your an image URL into a File() object.   This is great, however, I also want to manipulate the image using PIL as an Image() object. (or ImageFile object).  </p>
  
  <p>My preferred approach would be then to load the image URL directly into an Image() object, preform the resize, and convert it to a File() object and then save it in the model.  However, my attempts to convert an Image() to a File() have failed.  If at all possible, I want to limit the number of times I write to the disk, so I'd like to do this object transformation in Memory or using a NamedTemporaryFile(delete=True) object so I don't have to worry about extra files laying around.  (Of course, I want the file to be written to disk once it is saved via the model). </p>
  
  <pre><code>import urllib2
  from PIL import Image, ImageFile    
  from django.core.files import File
  from django.core.files.temp import NamedTemporaryFile
  
  inStream = urllib2.urlopen('http://www.google.com/intl/en_ALL/images/srpr/logo1w.png')
  
  parser = ImageFile.Parser()
  while True:
      s = inStream.read(1024)
      if not s:
          break
      parser.feed(s)
  
  inImage = parser.close()
  # convert to RGB to avoid error with png and tiffs
  if inImage.mode != "RGB":
      inImage = inImage.convert("RGB")
  
  # resize could occur here
  
  # START OF CODE THAT DOES NOT SEEM TO WORK
  # I need to somehow convert an image .....
  
  img_temp = NamedTemporaryFile(delete=True)
  img_temp.write(inImage.tostring())
  img_temp.flush()
  
  file_object = File(img_temp)
  
  # .... into a file that the Django object will accept. 
  # END OF CODE THAT DOES NOT SEEM TO WORK
  
  my_model_instance.image.save(
           'some_filename',
           file_object,  # this must be a File() object
           save=True,
           )
  </code></pre>
  
  <p>With this approach, the file appears corrupt whenever I view it as an image.  Does anyone have any approach that takes a file file from a URL, allows one to manipulate it as an Image and then save it to a Django ImageField?</p>
  
  <p>Any help is much appreciated. </p>
  
  <p><a href="https://stackoverflow.com/questions/1308386/programmatically-saving-image-to-django-imagefield">Programmatically saving image to Django ImageField</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1393202/django-add-image-in-an-imagefield-from-image-url">Django: add image in an ImageField from image url</a></p>
  
  <p>Update 08/11/2010: I did end up going with StringIO, however, I was stringIO was throwing an unusual Exception when I tried to save it in a Django ImageField.  Specifically, the stack trace showed a name error: </p>
  
  <pre><code>"AttribueError exception "StringIO instance has no attribute 'name'"
  </code></pre>
  
  <p>After digging through the Django source, it looks like this error was caused when the model save tries to access the size attribute of the StringIO "File".  (Though the error above indicates a problem with the name, the root cause of this error appears to be the lack of a size property on the StringIO image).  As soon as I assigned a value to the size attribute of the image file, it worked fine.  </p>