Saving animated GIFs using PIL (the saved image is not animated)

I have Apache2 + PIL + Django + X-sendfile. My problem is that when I save the animated GIF, it will not “animate” when I exit through the browser.

Here is my code for displaying an image located outside the public directory.

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

UPDATE

Turns out it works. My problem is that I am trying to upload an image at a url. It probably doesn't save the whole GIF?

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

Any ideas?

Thank!

Wenbert

+3
source share
2 answers

Where does the class come from Imageand what does it do Image.open?

, ( ), Gif.

Edit:

, PIL. PIL GIF :

PIL GIF87a GIF89a GIF. GIF87a, .

, im .

+3

, PIL. PIL, .

: , PIL, , , GIF, , , PIL.

GIF , HTTP-, .

, PIL GIF:

def image_is_animated_gif(image):
    # verify image format
    if image.format.lower() != 'gif':
        return False

    # verify GIF is animated by attempting to seek beyond the initial frame
    try:
        image.seek(1)
    except EOFError:
        return False
    else:
        return True
+1

Source: https://habr.com/ru/post/1784452/


All Articles