Python PIL fromstring错误

Python PIL fromstring错误,python,python-imaging-library,Python,Python Imaging Library,我有一张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:“参数1必须是没有空字节的

我有一张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:“参数1必须是没有空字节的字符串,而不是str”


如何解决这个问题?

您的论点颠倒过来了:

Image.fromstring(mode, size, data, decoder_name='raw', *args)
所以

但请注意,直接从
StringIO
对象读取要容易得多:

output = StringIO.StringIO()
old_image.save(output, format="PNG")

output.seek(0)
new_image = Image.open(output)
output = StringIO.StringIO()
old_image.save(output, format="PNG")

output.seek(0)
new_image = Image.open(output)