如何制作一组带有每个图像的工作时长的动画图像。

huangapple go评论95阅读模式
英文:

How to make an animated set of images with a working duration per image

问题

以下是翻译好的内容:

我想在Python中制作一组动画图像。我不能使用动画gif,因为我需要超过256种颜色。我已经尝试了动画png,但似乎持续时间参数不起作用。这是一个最小可行示例:

  1. import seaborn as sns
  2. import io
  3. import matplotlib.pyplot as plt
  4. import scipy
  5. import math
  6. import numpy as np
  7. import imageio
  8. vmin = 0
  9. vmax = 0.4
  10. images = []
  11. for i in range(3):
  12. mu = 0
  13. variance = i+0.1
  14. sigma = math.sqrt(variance)
  15. x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
  16. row = scipy.stats.norm.pdf(x, mu, sigma)
  17. matrix = []
  18. for _ in range(100):
  19. matrix.append(row)
  20. cmap = "viridis"
  21. hmap = sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap=cmap)
  22. hmap.set_xticklabels([*range(0, 100, 4)])
  23. cbar = hmap.collections[0].colorbar
  24. cbar.set_label("Colorbar Label", labelpad=10) # 设置标签并调整标签间距使用labelpad
  25. plt.savefig(f"image_{i}.png", format='png')
  26. plt.close()
  27. images.append(imageio.v3.imread(f"image_{i}.png"))
  28. imageio.mimwrite("out.png", images, duration=4)

有没有设置持续时间的方法,或者是否有另一种制作一组动画图像的方法,不需要除Python之外的外部工具?

英文:

I want to make an animated set of images in python. I can't use an animated gif as I need more than 256 colors. I have tried an animated png but it seems the duration argument doesn't work. Here is a MWE:

  1. import seaborn as sns
  2. import io
  3. import matplotlib.pyplot as plt
  4. import scipy
  5. import math
  6. import numpy as np
  7. import imageio
  8. vmin = 0
  9. vmax = 0.4
  10. images = []
  11. for i in range(3):
  12. mu = 0
  13. variance = i+0.1
  14. sigma = math.sqrt(variance)
  15. x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
  16. row = scipy.stats.norm.pdf(x, mu, sigma)
  17. matrix = []
  18. for _ in range(100):
  19. matrix.append(row)
  20. cmap = "viridis"
  21. hmap = sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap=cmap)
  22. hmap.set_xticklabels([*range(0, 100, 4)])
  23. cbar = hmap.collections[0].colorbar
  24. cbar.set_label("Colorbar Label", labelpad=10) # Set the label and adjust the spacing using labelpad
  25. plt.savefig(f"image_{i}.png", format='png')
  26. plt.close()
  27. images.append(imageio.v3.imread(f"image_{i}.png"))
  28. imageio.mimwrite("out.png", images, duration=4)

Is there way to set the duration, or alternatively, is there another way to make an animated set of images that doesn't require any external tools other than python?

答案1

得分: 2

你正在设置持续时间:

  1. imageio.mimwrite("out.png", images, duration=4)

根据Pillow文档,持续时间以毫秒表示。设置duration=1000会导致APNG变慢。

如何制作一组带有每个图像的工作时长的动画图像。

英文:

You are setting the duration:

  1. imageio.mimwrite("out.png", images, duration=4)

Per the Pillow docs, duration is expressed in milliseconds. Setting duration=1000 results in a slower APNG.

如何制作一组带有每个图像的工作时长的动画图像。

huangapple
  • 本文由 发表于 2023年6月8日 04:59:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定