title: Saving animated GIFs using PIL (image saved does not animate)
body: <p>I have Apache2 + PIL + Django + X-sendfile. My problem is that when I save an animated GIF, it won't "animate" when I output through the browser.</p>
  
  <p>Here is my code to display the image located outside the public accessible directory.</p>
  
  <pre><code>def raw(request,uuid):
      target = str(uuid).split('.')[:-1][0]
      image = Uploads.objects.get(uuid=target)
  
      path = image.path
      filepath = os.path.join(path,"%s.%s" % (image.uuid,image.ext))
  
      response = HttpResponse(mimetype=mimetypes.guess_type(filepath)) 
      response['Content-Disposition']='filename="%s"'\
                                      %smart_str(image.filename)
      response["X-Sendfile"] = filepath
      response['Content-length'] = os.stat(filepath).st_size
  
      return response
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>It turns out that it works. My problem is when I try to upload an image via URL. It probably doesn't save the entire GIF?</p>
  
  <pre><code>def handle_url_file(request):
      """
      Open a file from a URL.
      Split the file to get the filename and extension.
      Generate a random uuid using rand1()
      Then save the file.
      Return the UUID when successful.
      """
  
      try:
          file = urllib.urlopen(request.POST['url'])
          randname = rand1(settings.RANDOM_ID_LENGTH)
          newfilename = request.POST['url'].split('/')[-1]
          ext = str(newfilename.split('.')[-1]).lower()
          im = cStringIO.StringIO(file.read()) # constructs a StringIO holding the image
          img = Image.open(im)
  
          filehash = checkhash(im)
  
          image = Uploads.objects.get(filehash=filehash)
          uuid = image.uuid
  
          return "%s" % (uuid)
  
      except Uploads.DoesNotExist:
  
          img.save(os.path.join(settings.UPLOAD_DIRECTORY,(("%s.%s")%(randname,ext))))
          del img
  
          filesize = os.stat(os.path.join(settings.UPLOAD_DIRECTORY,(("%s.%s")%(randname,ext)))).st_size
          upload = Uploads(
              ip          = request.META['REMOTE_ADDR'],
              filename    = newfilename,
              uuid        = randname,
              ext         = ext,
              path        = settings.UPLOAD_DIRECTORY,
              views       = 1,
              bandwidth   = filesize,
              source      = request.POST['url'],
              size        = filesize,
              filehash    = filehash,
          )
  
          upload.save()
          #return uuid
          return "%s" % (upload.uuid)
      except IOError, e:
          raise e
  </code></pre>
  
  <p>Any ideas? </p>
  
  <p>Thanks! </p>
  
  <p>Wenbert</p>