Skip to content

Save GIF without loss of quality #8251

@zxbzxb180

Description

@zxbzxb180

I am currently using Pillow 9.4.0 and have noticed that the GifImagePlugin. py source code of Pillow uses this code to convert RGBA to P mode when processing GIFs, resulting in a loss of image quality. How can I resolve this issue?
im=im.convert("P", palette=Image. Palette. AdaPTIVE)
image
This is the input GIF
input
This is the saved GIF
output

Here is my code:

from PIL import Image, ImageSequence, GifImagePlugin
GifImagePlugin.LOADING_STRATEGY = GifImagePlugin.LoadingStrategy.RGB_AFTER_DIFFERENT_PALETTE_ONLY

gif = Image.open("input.gif")
frames = [frame.copy() for frame in ImageSequence.Iterator(gif)]
frames[0].save(
    "output.gif",
    save_all=True,
    append_images=frames[1:],
    optimize=False,
    disposal=2,
    lossless=True,
    loop=0
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions