Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@radarhere
Copy link
Member

Resolves #4097

The conclusion of discussion in the issue is that StringIO can't be used to pass bytes into Image.open.

import io
from PIL import Image
im = Image.open(io.StringIO("'\x89\x50\x4E\x47\x0D"))
OSError: cannot identify image file <_io.StringIO object at 0x104ecdd70>

A ValueError with a clearer message with now be raised in this situation, explaining that byte data is required instead of StringIO.

@kdschlosser
Copy link

question.... am I able to use this when calling image.open

image.open(b'\x65\x32\x86\x54\xD2\xA1')

the byte data is nothing just for example.

or does it have to explicitly be.

image.open(io.BytesIO(b'\x65\x32\x86\x54\xD2\xA1'))

@radarhere
Copy link
Member Author

@kdschlosser the second one

Image.open(io.BytesIO(b'\x65\x32\x86\x54\xD2\xA1'))

The first code sample can be used for a path -

Image.open(b"Tests/images/hopper.jpg")

radarhere and others added 3 commits December 26, 2019 19:54
Co-Authored-By: Hugo van Kemenade <[email protected]>
Co-Authored-By: Hugo van Kemenade <[email protected]>
@radarhere radarhere merged commit e5d4896 into python-pillow:master Dec 26, 2019
@radarhere radarhere deleted the stringio branch December 26, 2019 10:28
@kdschlosser
Copy link

the reason I bring that up is because of the error description..
"StringIO cannot be used to open an image. Binary data must be used instead"

byte literal and bytearray fall into the binary data category.. BytesIO kind of does but fits into the stream or file like object more so then being binary data..

This would be a better description "StringIO is not supported, please use BytesIO instead."
with the description above it provides the exact solution that works. There is no possibility of a user misunderstanding what needs to be used as a replacement for StringIO.

just a suggestion....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loading image from bytes

3 participants