I have a png picture, and i need to save it as string, and then open it again with PIL. I'm trying to do it like that:
output = StringIO.StringIO()
old_image.save(output, format="PNG")
contents = output.getvalue()
output.close()
new_image = Image.fromstring(contents, "RGBA", old_image.size)
but it gives me an error: TypeError: 'argument 1 must be string without null bytes, not str'
How to solve this problem?
You've got the arguments reversed:
Image.fromstring(mode, size, data, decoder_name='raw', *args)
so
Image.fromstring("RGBA", old_image.size, contents)
But note that it's much easier to read from the StringIO object directly:
output = StringIO.StringIO()
old_image.save(output, format="PNG")
output.seek(0)
new_image = Image.open(output)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With