Skip to content

First frame of transparent GIF is read incorrectly #6590

@jerrutledge

Description

@jerrutledge

What did you do?

I have a four-frame transparent GIF I want to do some manipulation with (i.e. comparing frames, cropping).
squidcat

I ran this code to turn each frame into an array, and saved each array as a png for visualization purposes.

from PIL import Image
import numpy

inputFileName = "squidcat.gif"
outputFileName = "pngcat"

frames = []
with Image.open(inputFileName, formats=["GIF"]) as imageObject:
    # save each frame to a numpy array
    for frame_num in range(0, imageObject.n_frames):
        imageObject.seek(frame_num)
        frames.append(numpy.array(imageObject))

# do something with the numpy frames

for i in range(len(frames)):
    # save each frame as a png to visualize
    fileName = outputFileName + "_" + str(i) + ".png"
    Image.fromarray(frames[i]).save(fileName)

What did you expect to happen?

I expected the program to generate four png files containing the four transparent frames.

What actually happened?

The first frame was completely black, with the subsequent three turning out normally. I've included my results below:
pngcat_0
pngcat_1
pngcat_2
pngcat_3

What are your OS, Python and Pillow versions?

  • OS: Mac OS 12.5.1 (21G83)
  • Python: 3.10.6
  • Pillow: 9.2.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