python - 在 Python 中将动画 GIF 转换为 4D 数组
问题描述
我想将 .gif 文件转换为 4D 数组/RGB 值的 3D 数组。
我试过 PIL,但这似乎只能让我将 gif 作为灰度图像读取。
ndimage
from numpy 没有这个问题,但它只会导致 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)])
输出:
(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)]
推荐阅读
- java - 在 Mac OS X for Java 应用程序上存储首选项、日志文件、缓存的位置
- python - Python Jupyter Notebook:如何从维基百科十六进制值(如 U+1F0A1)打印 unicode 字符?
- codeigniter - 如何在 CodeIgniter 中通过 URI 删除缓存的网页
- vb.net - 如何在特定列的文本框中获取特定行的值
- javascript - 如何解决 Dropbox Javascript 选择器中的 Malformed Origin 错误
- javascript - Multiple Axios GET requests from different components
- jupyter-notebook - 使用 Colaboratory 在笔记本的文本单元格中插入图像(与笔记本在同一文件夹中)
- python - 将另一列设置为索引后,将数据框的前一个索引保留为一列
- javascript - 给出一个 div 可以回退的默认文本
- javascript - GM.xmlHttpRequest : 无法理解 URL