更新时间:2021-12-14 19:36:22
我进一步研究了如何使用PIL/Pillow中包含的工具更优雅地检索/更新图像数据,并给出了以下简单的工作示例:
I looked a bit more into how to retrieve/update the image data more elegantly with the tools included in PIL/Pillow, and made this simple working example:
from PIL import BmpImagePlugin
import hashlib
from itertools import cycle
keys = hashlib.md5(b"aaaabbbb").digest()
input_image = BmpImagePlugin.BmpImageFile("img/tea.bmp")
# extract pure image data as bytes
image_data = input_image.tobytes()
# encrypt
image_data = bytes(a^b for a, b in zip(image_data, cycle(keys)))
# create new image, update with encrypted data and save
output_image = input_image.copy()
output_image.frombytes(image_data)
output_image.save("img/tea-encrypted.bmp")
这样,您不必担心BMP标头的大小.
This way, you don't have to worry about the BMP header size.