Use BytesIO instead of StringIO (#4376)

Use BytesIO instead of StringIO
This commit is contained in:
Hugo van Kemenade 2020-01-21 14:32:15 +02:00 committed by GitHub
commit 8e332856dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -464,17 +464,17 @@ Reading from an open file
with open("hopper.ppm", "rb") as fp: with open("hopper.ppm", "rb") as fp:
im = Image.open(fp) im = Image.open(fp)
To read an image from string data, use the :py:class:`~StringIO.StringIO` To read an image from binary data, use the :py:class:`~io.BytesIO`
class: class:
Reading from a string Reading from binary data
^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
:: ::
from PIL import Image from PIL import Image
import StringIO import io
im = Image.open(StringIO.StringIO(buffer)) im = Image.open(io.BytesIO(buffer))
Note that the library rewinds the file (using ``seek(0)``) before reading the Note that the library rewinds the file (using ``seek(0)``) before reading the
image header. In addition, seek will also be used when the image data is read image header. In addition, seek will also be used when the image data is read