在 Python 中将动画 GIF 转换为 4D 数组

Convert Animated GIF to 4D Array in Python

我想将 .gif 文件转换为 4D array/a RGB 值的 3D 数组。

我已经尝试过 PIL,但这似乎只能让我以灰度图像的形式阅读 gif。 ndimage 来自 numpy 的没有这个问题,但它只会导致 gif 的第一帧。

出于测试目的,我尝试转换的 .gif 是

(非常小,在 GIMP 中制作)

它只是在第一帧的顶部增加了红色,在第一帧的侧面增加了绿色,随着帧的前进在左上角增加了蓝色。

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)])

输出:

(frame_num * frame_width * frame_height * 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)]