首页 > 解决方案 > 在 python 中使用 Pillow 将图像列表转换为 gif 时,仅在 gif 中显示最后一帧

问题描述

我正在尝试使用类似于小瓷砖图像的不同图像创建马赛克 jpg。我可以正确保存最终的马赛克,但我也试图通过使用列表保存循环中每个马赛克的快照来创建正在创建马赛克的过程的 gif,但是当我创建 gif 时,我只能看到最后一帧。请帮忙。谢谢!

from PIL import Image
import sys
mosaic = Image.new('RGB', (int(tilesX * tileHeight), int(tilesY * tileHeight)))
mosaicSnapList = []
count = 0
for x in range(0, width, pixelsPerTile):
  for y in range(0, height, pixelsPerTile):
    avg_color = findAverageImageColorInBox(baseImage, x, y, pixelsPerTile, pixelsPerTile)
    replacement = findBestTile(df_tiles, avg_color["avg_r"], avg_color["avg_g"], avg_color["avg_b"])

    tile = DISCOVERY.getTileImage(replacement["fileName"].values[0], tileHeight)
    mosaic.paste(tile, (int(x / pixelsPerTile) * tileHeight, int(y / pixelsPerTile) * tileHeight))
    mosaicSnapList.append(mosaic)
    
  
  # Print out a progress message:
  curRow = int((x / pixelsPerTile) + 1)
  pct = (curRow / tilesX) * 100
  sys.stdout.write(f'\r  ...progress: {curRow * tilesY} / {tilesX * tilesY} ({pct:.2f}%)')

##  FUNCTION TO CONVERT LIST OF IMAGES TO GIF  ##
mosaicSnapList[0].save("try.gif",format='GIF',SAVE_ALL=True,append_images=mosaicSnapList[1:], duration=400, loop=0)

标签: pythonpython-imaging-library

解决方案


I think you want save_all=True, rather than SAVE_ALL=True in:

mosaicSnapList[0].save("try.gif",format='GIF',SAVE_ALL=True ...

推荐阅读