Skip to content

Reading GIF and saving it introduces artifacts #5307

@FirefoxMetzger

Description

@FirefoxMetzger

What did you do?

I'm trying to read a .gif into a numpy array to obtain a sequence of frames and then write the result back to disk as a new gif.

What did you expect to happen?

The image sequence loads and each frame contains a (fully rendered?) image. The created .gif and the original one look alike (minus FPS) upon visual inspection.

What actually happened?

The written/created .gifcontains artifacts, which (I suppose) come from either the writer not using the appropriate disposal method (1 - do not dispose), the reader only reading data from the current frame and disposing of the previous frame(s) each time, or the image containing transparent pixels which would then show the underlying frame/background.

What are your OS, Python and Pillow versions?

  • OS: Windows Build 19042.844
  • Python: Python 3.8.8
  • Pillow: Pillow==8.1.0
import numpy as np
from PIL import Image, ImageSequence
import urllib.request

url = "https://github.com/imageio/imageio-binaries/blob/master/images/newtonscradle.gif?raw=true"

# For (visual) comparison, store the original GIF
response = urllib.request.urlopen(url)
with open("original.gif", "wb+") as file:
    file.write(response.read())

# load the image into a numpy array (via pillow) and write it to dist
response = urllib.request.urlopen(url)
pil_image = Image.open(response)

# It seems I am missing an option here
np_image = np.asarray([np.array(frame.convert("RGBA")) for frame in ImageSequence.Iterator(pil_image)])
pil_frames = [Image.fromarray(arr) for arr in np_image]

pil_frames[0].save("test.gif", loop=0, fps=60, save_all=True, append_images=pil_frames[1:])

Original Image:
original

Resulting Image:
test

It might be a user error on my part and I am simply missing an option. In either case, I don't think the default behavior should change, but it would be good to have the option to make a frame contain the same pixel values that a user would see once the frame is displayed (including non-disposed pixels of the previous frame(s)).

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