如何在 Pygame 中存储像素数组

How to store arrays of pixels in Pygame

我正在尝试使用 Pygame 在 Python 中创建一个小型图像编辑程序。我想存储应用程序正在处理的像素数组,这样我就可以拥有 undo/redo 功能和缩放。我该怎么做?

我尝试将像素存储为列表中列表中的 rgb 值列表,如下所示:

x1 = [[255, 255, 255]]
x2 = [[255, 0, 0]]
y1 = [x1, x2]

但我很难相信这是最好的方法。

关于 undo/redo,请检查 Command Design Pattern(python 示例)。

由于您没有提供太多代码,基本方法可能依赖于维护一个包含您的坐标的唯一列表。应该更改该列表的一件事是 Command,它将包含 undoredo 方法来恢复列表状态。

更新

我不确定我是否完全理解您想要实现的目标,但是 dictionary 可能对您有用:

coords = {}
coords['y1'] = [[255,255,255],[255,0,0]] #add dynamic keys to your dictionary and fill them with the right values

如果以后需要存储传输您的数据,您也可以在这里获得加号,您可以简单地在JSON 格式如下:

import json
... 
json.dumps(coords) #returns the json string containing your data