python - 在 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)
解决方案
I think you want save_all=True
, rather than SAVE_ALL=True
in:
mosaicSnapList[0].save("try.gif",format='GIF',SAVE_ALL=True ...
推荐阅读
- angular - 是否可以监听组件中的事件并更改本机元素?
- postgresql - PostgreSQL \copy from ... with CSV null as '' 仍然导入空字符串而不是 NULL
- python - 将带有日期的 Pandas DataFrame 转换为 Spark DataFrame 时出错
- python - 使用 Python 读取串行接口会返回失真的数据
- django - Django将参数传递给Form,得到KeyError
- python - GridSearchCV 使用多个分类器:TypeError: __init__() 缺少 1 个必需的位置参数:'estimator'
- continuous-integration - 我应该如何在我的生产服务器上部署 CI/CD 管道的结果
- javascript - this.setState() 不会立即更新数据
- jmeter - Taurus - 运行 jmx 测试和 yml 测试时出错
- c++ - 无法退出无限循环