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

Raise ValueError for io.StringIO in Image.open #4302

Merged
merged 4 commits into from Dec 26, 2019

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")

src/PIL/Image.py Outdated Show resolved Hide resolved
src/PIL/Image.py Outdated Show resolved Hide resolved
radarhere and others added 3 commits December 26, 2019 19:54
Co-Authored-By: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-Authored-By: Hugo van Kemenade <hugovk@users.noreply.github.com>
@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