【发布时间】:2017-06-10 13:31:24
【问题描述】:
我在 python 中有一个字节数组(从任意文本文件转换而来),并希望将这些字节用作 RGB 值来存储在图像中。做这个的最好方式是什么?谢谢
【问题讨论】:
我在 python 中有一个字节数组(从任意文本文件转换而来),并希望将这些字节用作 RGB 值来存储在图像中。做这个的最好方式是什么?谢谢
【问题讨论】:
这是一种迟到的回应,但它可能会在未来对其他人有所帮助:希望我对您的问题的解释是正确的,但是如果您的“任意文本文件”代表像“.jpg”这样的图像文件的结构,您可以更改文件扩展名从 ".txt" 到 ".jpg" 并使用 PIL 导入。
你可以这样做:
from PIL import Image
path_to_file = 'path/to/arbitraty_textfile.txt'
safe_path = path_to_file.replace('.txt','.jpg')
with open(path_to_file,'rb') as textfile:
bytestring = textfile.read()
with open(safe_path, 'wb') as imagefile:
imagefile.write(bytestring)
#Import with PIL
image = Image.open(safe_path)
# ...
如果你想在python中读取或写入一串字节,属性'rb'或'wb'是这里的关键词。
让我知道这是否接近解决方案,我猜你已经找到了。
【讨论】: