Accept io.StringIO as a path for Image.open

This commit is contained in:
Andrew Murray 2019-12-23 12:31:34 +11:00
parent 33cfac7715
commit e8c56f5e1b
3 changed files with 17 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import io
import os
import shutil
import tempfile
@ -91,6 +92,11 @@ class TestImage(PillowTestCase):
def test_bad_mode(self):
self.assertRaises(ValueError, Image.open, "filename", "bad mode")
def test_stringio(self):
with Image.open(io.StringIO("Tests/images/hopper.jpg")) as im:
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
def test_pathlib(self):
from PIL.Image import Path

View File

@ -3,10 +3,10 @@
File Handling in Pillow
=======================
When opening a file as an image, Pillow requires a filename, ``pathlib.Path``
object, or a file-like object. Pillow uses the filename or ``Path`` to open a
file, so for the rest of this article, they will all be treated as a file-like
object.
When opening a file as an image, Pillow requires a filename, ``io.StringIO``
object, ``pathlib.Path`` object, or a file-like object. Pillow uses the
filename, ``StringIO`` or ``Path`` to open a file, so for the rest of this
article, they will all be treated as a file-like object.
The following are all equivalent::
@ -17,6 +17,9 @@ The following are all equivalent::
with Image.open('test.jpg') as im:
...
with Image.open(io.StringIO('test.jpg')) as im2:
...
with Image.open(pathlib.Path('test.jpg')) as im2:
...

View File

@ -2674,8 +2674,8 @@ def open(fp, mode="r"):
:py:meth:`~PIL.Image.Image.load` method). See
:py:func:`~PIL.Image.new`. See :ref:`file-handling`.
:param fp: A filename (string), pathlib.Path object or a file object.
The file object must implement :py:meth:`~file.read`,
:param fp: A filename (string), io.StringIO object, pathlib.Path object
or a file object. The file object must implement :py:meth:`~file.read`,
:py:meth:`~file.seek`, and :py:meth:`~file.tell` methods,
and be opened in binary mode.
:param mode: The mode. If given, this argument must be "r".
@ -2691,6 +2691,8 @@ def open(fp, mode="r"):
filename = ""
if isinstance(fp, Path):
filename = str(fp.resolve())
elif isinstance(fp, io.StringIO):
filename = fp.getvalue()
elif isPath(fp):
filename = fp