Nic*_*mer 5 python png image-processing sparse-matrix python-imaging-library
我想在Python中存储PNG图像,其中RGB值由列表给出
entries = [
[1, 2, [255, 255, 0]],
[1, 5, [255, 100, 0]],
[2, 5, [0, 255, 110]],
# ...
]
Run Code Online (Sandbox Code Playgroud)
(行,列,RGB三元组),以及[255, 255, 255]有关图像总尺寸的默认值和信息.
使用PIL,我当然可以转换entries为密集的m-by n-by- 3matrix,但这不适合内存; 矩阵维数可以在万里.
是否有其他方法可以使用上述信息创建PNG图像?
该PurePNG库写入文件中的行由行,只需要一排迭代器:
def write_png(A, filename):
m, n = A.shape
w = png.Writer(n, m, greyscale=True, bitdepth=1)
class RowIterator:
def __init__(self, A):
self.A = A.tocsr()
self.current = 0
return
def __iter__(self):
return self
def __next__(self):
if self.current+1 > A.shape[0]:
raise StopIteration
out = numpy.ones(A.shape[1], dtype=bool)
out[self.A[self.current].indices] = False
self.current += 1
return out
with open(filename, 'wb') as f:
w.write(f, RowIterator(A))
return
Run Code Online (Sandbox Code Playgroud)