Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL fromstring error

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?

like image 524
nukl Avatar asked Dec 15 '25 16:12

nukl


1 Answers

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)
like image 54
Fred Foo Avatar answered Dec 17 '25 08:12

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!