from PIL import Image
import numpy as np
Image.MAX_IMAGE_PIXELS = 933120000
# Load image as PIL Image and make into Numpy array
im = Image.open('eta.jpg')
na = np.array(im)
# Mask LSBs and ensure small storage allocation of np.uint8
LSBs = (na & 1).astype(np.uint8)
LSBs.tofile('filename.bin')
# Generate some random data
LSBs = np.random.randint(2, size=(20_000, 14_000, 3), dtype=np.uint8).tobytes()
import gzip
compressed = gzip.compress(LSBs)
from pathlib import Path
Path('compressed.bin').write_bytes(compressed)
如果您想将 LSB 保存为 PNG,请从我的原始代码开始,并将其添加到末尾:
# Mask LSBs and ensure small storage allocation of np.uint8
LSBs = (na & 1).astype(np.uint8)
# Make LSBs into PIL Image and save
pi = Image.fromarray(LSBs)
pi.save('LSBs.png')
不确定你期望的速度是多少,但使用Pillow可以像这样简单:
请注意,您可以在写入之前压缩数据。以下是代码,
gzip
但您可以将单词更改gzip
为lzma
或bz2
尝试其他方法:如果您想将 LSB 保存为 PNG,请从我的原始代码开始,并将其添加到末尾:
您还可以尝试将最后一行替换为: