PIL fromstring 错误

3

我有一张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'

如何解决这个问题?

1个回答

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接