mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-07 13:25:24 +03:00
More support for arbitrary os.PathLike
This commit is contained in:
parent
d631afc266
commit
61d47c3dfa
|
@ -7,6 +7,7 @@ import shutil
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import warnings
|
import warnings
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
@ -161,8 +162,6 @@ class TestImage:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_pathlib(self, tmp_path):
|
def test_pathlib(self, tmp_path):
|
||||||
from PIL.Image import Path
|
|
||||||
|
|
||||||
with Image.open(Path("Tests/images/multipage-mmap.tiff")) as im:
|
with Image.open(Path("Tests/images/multipage-mmap.tiff")) as im:
|
||||||
assert im.mode == "P"
|
assert im.mode == "P"
|
||||||
assert im.size == (10, 10)
|
assert im.size == (10, 10)
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
File Handling in Pillow
|
File Handling in Pillow
|
||||||
=======================
|
=======================
|
||||||
|
|
||||||
When opening a file as an image, Pillow requires a filename, ``pathlib.Path``
|
When opening a file as an image, Pillow requires a filename, ``os.PathLike``
|
||||||
object, or a file-like object. Pillow uses the filename or ``Path`` to open a
|
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
|
file, so for the rest of this article, they will all be treated as a file-like
|
||||||
object.
|
object.
|
||||||
|
|
|
@ -39,7 +39,6 @@ import tempfile
|
||||||
import warnings
|
import warnings
|
||||||
from collections.abc import Callable, MutableMapping
|
from collections.abc import Callable, MutableMapping
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from defusedxml import ElementTree
|
from defusedxml import ElementTree
|
||||||
|
@ -2370,7 +2369,7 @@ class Image:
|
||||||
implement the ``seek``, ``tell``, and ``write``
|
implement the ``seek``, ``tell``, and ``write``
|
||||||
methods, and be opened in binary mode.
|
methods, and be opened in binary mode.
|
||||||
|
|
||||||
:param fp: A filename (string), pathlib.Path object or file object.
|
:param fp: A filename (string), os.PathLike object or file object.
|
||||||
:param format: Optional format override. If omitted, the
|
:param format: Optional format override. If omitted, the
|
||||||
format to use is determined from the filename extension.
|
format to use is determined from the filename extension.
|
||||||
If a file object was used instead of a filename, this
|
If a file object was used instead of a filename, this
|
||||||
|
@ -2385,11 +2384,8 @@ class Image:
|
||||||
|
|
||||||
filename = ""
|
filename = ""
|
||||||
open_fp = False
|
open_fp = False
|
||||||
if isinstance(fp, Path):
|
if is_path(fp):
|
||||||
filename = str(fp)
|
filename = os.fspath(fp)
|
||||||
open_fp = True
|
|
||||||
elif is_path(fp):
|
|
||||||
filename = fp
|
|
||||||
open_fp = True
|
open_fp = True
|
||||||
elif fp == sys.stdout:
|
elif fp == sys.stdout:
|
||||||
try:
|
try:
|
||||||
|
@ -3206,7 +3202,7 @@ def open(fp, mode="r", formats=None) -> Image:
|
||||||
:py:meth:`~PIL.Image.Image.load` method). See
|
:py:meth:`~PIL.Image.Image.load` method). See
|
||||||
:py:func:`~PIL.Image.new`. See :ref:`file-handling`.
|
:py:func:`~PIL.Image.new`. See :ref:`file-handling`.
|
||||||
|
|
||||||
:param fp: A filename (string), pathlib.Path object or a file object.
|
:param fp: A filename (string), os.PathLike object or a file object.
|
||||||
The file object must implement ``file.read``,
|
The file object must implement ``file.read``,
|
||||||
``file.seek``, and ``file.tell`` methods,
|
``file.seek``, and ``file.tell`` methods,
|
||||||
and be opened in binary mode. The file object will also seek to zero
|
and be opened in binary mode. The file object will also seek to zero
|
||||||
|
@ -3244,8 +3240,8 @@ def open(fp, mode="r", formats=None) -> Image:
|
||||||
|
|
||||||
exclusive_fp = False
|
exclusive_fp = False
|
||||||
filename = ""
|
filename = ""
|
||||||
if isinstance(fp, Path):
|
if isinstance(fp, os.PathLike):
|
||||||
filename = str(fp.resolve())
|
filename = os.path.realpath(os.fspath(fp))
|
||||||
elif is_path(fp):
|
elif is_path(fp):
|
||||||
filename = fp
|
filename = fp
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user