我想将 .gif 文件转换为 4D 数组/RGB 值的 3D 数组。
我尝试过 PIL,但这似乎只能让我将 gif 读取为灰度图像。 numpy 中的 ndimage 没有这个问题,但它只产生 gif 的第一帧。
出于测试目的,我尝试转换的 .gif 是
随着帧的前进,第一帧顶部的红色颜色会增加,第一帧侧面的绿色颜色会增加,左上角的蓝色颜色会增加。
PIL 给我
[[[ 0 3 5 6 8]
[ 7 12 12 12 12]
[ 9 12 12 12 12]
[10 12 12 12 12]
[11 12 12 12 12]]
[[ 1 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]]
[[ 2 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]]
[[ 4 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]
[12 12 12 12 12]]]
而 numpy 给了我
[[[ 0 0 0]
[ 20 0 0]
[ 40 0 0]
[ 60 0 0]
[ 80 0 0]]
[[ 0 20 0]
[255 255 255]
[255 255 255]
[255 255 255]
[255 255 255]]
[[ 0 40 0]
[255 255 255]
[255 255 255]
[255 255 255]
[255 255 255]]
[[ 0 60 0]
[255 255 255]
[255 255 255]
[255 255 255]
[255 255 255]]
[[ 0 80 0]
[255 255 255]
[255 255 255]
[255 255 255]
[255 255 255]]]
这两者都不是我想要的。
最佳答案
import numpy as np
from PIL import Image, ImageSequence
img = Image.open('test.gif')
frames = np.array([np.array(frame.copy().convert('RGB').getdata(),dtype=np.uint8).reshape(frame.size[1],frame.size[0],3) for frame in ImageSequence.Iterator(img)])
输出:
(帧编号 * 帧宽度 * 帧高度 * 3(RGB) )
[array([[[ 0, 0, 0],
[ 20, 0, 0],
[ 40, 0, 0],
[ 60, 0, 0],
[ 80, 0, 0]],
[[ 0, 20, 0],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[ 0, 40, 0],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[ 0, 60, 0],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[ 0, 80, 0],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]], dtype=uint8),
array([[[ 0, 0, 20],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]], dtype=uint8),
array([[[ 0, 0, 40],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]], dtype=uint8),
array([[[ 0, 0, 60],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]], dtype=uint8)]
关于python - 在 Python 中将动画 GIF 转换为 4D 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50054187/