英文:
Why isn't the full palette being used in an animated gif?
问题
I'm here to provide the translated content as requested. Here's the translated code portion:
我正在使用Pillow制作热图的动画gif。由于有许多不同的值,每一帧只能有256种颜色,我希望Pillow可以为每一帧提供不同的调色板。但似乎它没有使用大部分可用的颜色。以下是一个最小化的示例:
from PIL import Image
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, 400)
row = scipy.stats.norm.pdf(x, mu, sigma)
matrix = []
for i in range(400):
matrix.append(row)
cmap = "viridis"
hmap = sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap=cmap, cbar=False)
hmap.set_xticks(range(0, 101, 4))
buffer = io.BytesIO()
plt.savefig(buffer, format='png')
buffer.seek(0)
images.append(Image.open(buffer))
plt.clf()
images[0].save("out.gif", save_all=True, duration=1000, loop=1, append_images=images[1:])
Please note that the translated code above is the same as the original code, but in Chinese.
英文:
I am making an animated gif of heatmaps using pillow. As there are a lot of different values and each frame can only have 256 colors I was hoping pillow would give me a different palette per frame. But it seems it isn't using most of the available colors. Here is a MWE:
from PIL import Image
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, 400)
row = scipy.stats.norm.pdf(x, mu, sigma)
matrix = []
for i in range(400):
matrix.append(row)
cmap = "viridis"
hmap = sns.heatmap(matrix, vmin=vmin, vmax=vmax, cmap=cmap, cbar=False)
hmap.set_xticks(range(0, 101, 4))
buffer = io.BytesIO()
plt.savefig(buffer, format='png')
buffer.seek(0)
images.append(Image.open(buffer))
plt.clf()
images[0].save("out.gif", save_all=True, duration=1000, loop=1, append_images=images[1:])
The animated gif produced is:
You can see that later frames use fewer than 256 colors. If I look at the palettes with
identify -verbose out.gif|grep Colors
I get these:
- For the first frame: Colors: 46
- For the second frame: Colors: 28
- For the third frame: Colors: 19
If I save png's instead we can see the third frame, for example, has many more than 19 colors.
What am I doing wrong?
答案1
得分: 1
Matplotlib正在生成调色板图像,这导致PIL继续在相同的轨道上运行。通过使用以下方式给PIL更多的自由度:
images.append(Image.open(buffer).convert('RGB'))
你将会得到:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论