英文:
How to make an animated set of images with a working duration per image
问题
以下是翻译好的内容:
我想在Python中制作一组动画图像。我不能使用动画gif,因为我需要超过256种颜色。我已经尝试了动画png,但似乎持续时间参数不起作用。这是一个最小可行示例:
import seaborn as sns
import io
import matplotlib.pyplot as plt
import scipy
import math
import numpy as np
import imageio
vmin = 0
vmax = 0.4
images = []
for i in range(3):
mu = 0
variance = i+0.1
sigma = math.sqrt(variance)
x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
row = scipy.stats.norm.pdf(x, mu, sigma)
matrix = []
for _ in range(100):
matrix.append(row)
cmap = "viridis"
hmap = sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap=cmap)
hmap.set_xticklabels([*range(0, 100, 4)])
cbar = hmap.collections[0].colorbar
cbar.set_label("Colorbar Label", labelpad=10) # 设置标签并调整标签间距使用labelpad
plt.savefig(f"image_{i}.png", format='png')
plt.close()
images.append(imageio.v3.imread(f"image_{i}.png"))
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:
import seaborn as sns
import io
import matplotlib.pyplot as plt
import scipy
import math
import numpy as np
import imageio
vmin = 0
vmax = 0.4
images = []
for i in range(3):
mu = 0
variance = i+0.1
sigma = math.sqrt(variance)
x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
row = scipy.stats.norm.pdf(x, mu, sigma)
matrix = []
for _ in range(100):
matrix.append(row)
cmap = "viridis"
hmap = sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap=cmap)
hmap.set_xticklabels([*range(0, 100, 4)])
cbar = hmap.collections[0].colorbar
cbar.set_label("Colorbar Label", labelpad=10) # Set the label and adjust the spacing using labelpad
plt.savefig(f"image_{i}.png", format='png')
plt.close()
images.append(imageio.v3.imread(f"image_{i}.png"))
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
你正在设置持续时间:
imageio.mimwrite("out.png", images, duration=4)
根据Pillow文档,持续时间以毫秒表示。设置duration=1000
会导致APNG变慢。
英文:
You are setting the duration:
imageio.mimwrite("out.png", images, duration=4)
Per the Pillow docs, duration is expressed in milliseconds. Setting duration=1000
results in a slower APNG.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论