Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BytesIO png to surface (cairo.IOError: error while reading from input stream) #245

Closed
maslaff opened this issue Dec 10, 2021 · 3 comments
Closed
Labels

Comments

@maslaff
Copy link

maslaff commented Dec 10, 2021

bytes = BytesIO()
# save png to BytesIO from PilImage
img.save(bytes, format='png')
imgsf = cairo.ImageSurface.create_from_png(bytes)

cairo.IOError: error while reading from input stream

Through file normally work. E.g.:

img.save('img.png', format='png')
imgsf = cairo.ImageSurface.create_from_png('img.png')

Ok

@dolfandringa
Copy link

dolfandringa commented Mar 5, 2022

I am fetching a png image online in a jupyter notebook and save it to io.BytesIO.
If I display it with in a jupyter notebook with

display(Image(background.getvalue(), width=width))

it shows up just fine, but if I

surface=cairo.ImageSurface.create_from_png(background)

if shows me the same error. For some reason pycairo can not read from any file-like object.

EDIT:
I just noticed, if I open a file with f=open('filename.png', 'w+b') then write and image to it, then f.seek(0) and then pass that file like object to create_from_png, it fails as well with the same error. But passing just the filename to create_from_png works just fine.

@lazka
Copy link
Member

lazka commented Mar 7, 2022

You need to seek to where you want to read from first, otherwise pycairo will only find an empty file object:

bytes = BytesIO()
# save png to BytesIO from PilImage
img.save(bytes, format='png')

bytes.seek(0)

imgsf = cairo.ImageSurface.create_from_png(bytes)

@lazka lazka added the needinfo label Mar 7, 2022
@maslaff
Copy link
Author

maslaff commented Mar 13, 2022

bytes.seek(0)

This is work! Thanks!

@maslaff maslaff closed this as completed Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants