我有一张png图片,我需要将其保存为字符串,然后使用PIL再次打开它。我试着这样做:
output = StringIO.StringIO()
old_image.save(output, format="PNG")
contents = output.getvalue()
output.close()
new_image = Image.fromstring(contents, "RGBA", old_image.size)
但它给了我一个错误:TypeError: 'argument 1 must be string without null bytes, not str'
如何解决这个问题?
答案 0 :(得分:3)
你的论点已被颠倒过来了:
Image.fromstring(mode, size, data, decoder_name='raw', *args)
所以
Image.fromstring("RGBA", old_image.size, contents)
但请注意,直接从StringIO
对象中读取更容易:
output = StringIO.StringIO()
old_image.save(output, format="PNG")
output.seek(0)
new_image = Image.open(output)