我在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)
# ...