To do the first step, there appears to be some detail that needs to be attended to. gif file, resize each frame one at a time and then reassemble them up again. To achieve what you want, I believe you have to first extract every frame from the. New_frame.thumbnail(resize_to, Image.ANTIALIAS)Īccording to Pillow 4.0x, the Image.resize function only works on a single image/frame. If so, we need to construct the new frame by pasting it on top of the preceding frames. Is this file a "partial"-mode GIF where frames update a region of a different size to the entire image? If not, we need to apply the global palette to the new frame. If the GIF uses local colour tables, each frame will have its own palette. # print("saving %s (%s) frame %d, %s %s" % (path, mode, i, im.size, im.tile)) Iterate the GIF, extracting each frame and resizing them Update_region_dimensions = update_regionĭef extract_and_resize_frames(path, resize_to=None): Necessary as assessing single frames isn't reliable. Pre-process pass over the image to determine the mode (full or additive). If not set, the original GIF will be resized toĪll_frames = extract_and_resize_frames(path, resize_to)Īll_frames.save(save_as, optimize=True)Īll_frames.save(save_as, optimize=True, save_all=True, append_images=all_frames, loop=1000) Resize_to (optional): new size of the gif. If not set, the original gif will be overwritten. Save_as (optional): Path of the resized gif. Only the first frame was displayed (you can see it here).įull code (should the above gist be deleted): def resize_gif(path, save_as=None, resize_to=None): Likewise, while imgur seems to make up for the speed problem, the GIF wouldn't display correctly when I tried to upload it to stack.imgur.While the GIF displays just fine when hosted by imgur, there is a speed issue when I open it from my computer where the entire GIF only take 1.5 seconds.The is not perfect so feel free to fork and improve it. It is using the thumbnail method of Pillow rather than the resize method as I found the resize method did not work. Using BigglesZX's script, I have created a new script which resizes a GIF using Pillow. When I open the gif file from my computer, the entire gif lasts about 1.5 seconds. Imgur made it slower (and converted it to mp4). Gif after processing ( out.gif) (I could not add it to Stack Overflow ). The gif speed is increased and the gif does not loop. I am not sure that the resize method works, as out.gif is still 7.5MB. The new gif is now saved and works, but there is 2 main problems : I've been saving all the frames together: img.save("out.gif", save_all=True, optimize=True). I have been resicing each frame individually: frame.resize((frame.size // 2, frame.size // 2), Image.ANTIALIAS) Running 2to3 -w gifextract.py makes that script compatible with Python 3. It is useful to note that this is a Python 2 script, and my project is written in Python 3 (I did mention that detail initially, but it was edited out by the Stack Overflow Community). I am using BigglesZX's script to extract all frames. Would you know how I can use Pillow to decrease the size of my GIF while keeping all of its frames?įollowing Old Bear's response, I have done the following changes: I've been thinking about calling resize() on im.seek() or im.tell() but neither of these methods return an Image object, and therefore I cannot call resize() on their output. im = im.resize((im.size // 2, im.size // 2), Image.ANTIALIAS) # should decrease its size If I add the following line, then only the first frame of the GIF is saved, instead of all of its frame. Im.save("out.gif", save_all=True, optimize=True, quality=10) # should decrease its quality Im.seek(im.tell() + 1) # loads all frames The following piece of code even makes the out.gif bigger than the initial gif: im = Image.open("my_gif.gif") With a GIF, though, it does not seem to work. Im.save("out.jpg", optimize=True, quality=85) # decreases its quality Im = im.resize((im.size // 2, im.size // 2), Image.ANTIALIAS) # decreases width and height of the image With JPEG, the following piece of code is usually enough so that large image drastically decrease in size. Resize it so its height / width is smaller I have a gif that I would like to resize with pillow so that its size decreases.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |