1
votes

I want to convert a .gif file into a 4D array/a 3D array of RGB values.

I've tried PIL, but that seems to only let me read the gif as a gray-scale image. ndimage from numpy doesn't have that problem, but it only results in the first frame of the gif.

The .gif I'm trying to convert, for testing purposes, is

enter image description here (very small, made in GIMP)

Which just increases in red across the top of the first frame, increases in green down the side of the fisrt frame, and increases in blue at the top left as the frames advace.

PIL gives me

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

Whereas numpy gives me

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

Neither of which is what I want.

1

1 Answers

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

output:

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