Anshu Agrawal
Anshu Agrawal

Reputation: 41

Getting AttributeError: im must have seek method while trying to save as GIF

I am trying to save a bunch of images in a folder as a gif.

My code is as follows:

from PIL import Image, ImageDraw
from PIL import ImageSequence 
import imageio


im1 = Image.open('../input/imageslaptop/temp2.png')
images = []
for k in range(3,53):
    f="../input/imageslaptop/temp"+str(k)+".png"
    images.append(np.asarray(Image.open(f)))
images = np.array(images)
im1.save("out.gif", save_all=True, append_images=images, duration=5100, loop=0)
print (version.parse(Image.PILLOW_VERSION) )

gives 8.0.1

error

Upvotes: 2

Views: 2115

Answers (1)

furas
furas

Reputation: 142804

You get error because you convert Pillow images to numpy arrays

Code works correctly without np.asarray and np.array

from PIL import Image

im1 = Image.open('../input/imageslaptop/temp2.png')

images = []

for k in range(3, 53):
    path = "../input/imageslaptop/temp" + str(k) + ".png"
    images.append(Image.open(path))

im1.save("out.gif", save_all=True, append_images=images, duration=5100, loop=0)

EDIT:

If you need to convert Pillow images to numpy arrays to make some changes then you have to convert arrays back to Pillow images before saving.

images = [Image.fromarray(img) for img in images]

from PIL import Image
import numpy as np

im1 = Image.open('../input/imageslaptop/temp2.png')

images = []

for k in range(3, 53):
    path = "../input/imageslaptop/temp" + str(k) + ".png"
    images.append(np.asarray(Image.open(path)))

# ... here make some change on images ... 

images = [Image.fromarray(img) for img in images]

im1.save("out.gif", save_all=True, append_images=images, duration=5100, loop=0)

Upvotes: 3

Related Questions