More support for arbitrary os.PathLike

This commit is contained in:
Hugo van Kemenade 2024-01-27 14:06:06 +02:00
parent d631afc266
commit 61d47c3dfa
3 changed files with 8 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import shutil
import sys
import tempfile
import warnings
from pathlib import Path
import pytest
@ -161,8 +162,6 @@ class TestImage:
pass
def test_pathlib(self, tmp_path):
from PIL.Image import Path
with Image.open(Path("Tests/images/multipage-mmap.tiff")) as im:
assert im.mode == "P"
assert im.size == (10, 10)

View File

@ -3,7 +3,7 @@
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
file, so for the rest of this article, they will all be treated as a file-like
object.

View File

@ -39,7 +39,6 @@ import tempfile
import warnings
from collections.abc import Callable, MutableMapping
from enum import IntEnum
from pathlib import Path
try:
from defusedxml import ElementTree
@ -2370,7 +2369,7 @@ class Image:
implement the ``seek``, ``tell``, and ``write``
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
format to use is determined from the filename extension.
If a file object was used instead of a filename, this
@ -2385,11 +2384,8 @@ class Image:
filename = ""
open_fp = False
if isinstance(fp, Path):
filename = str(fp)
open_fp = True
elif is_path(fp):
filename = fp
if is_path(fp):
filename = os.fspath(fp)
open_fp = True
elif fp == sys.stdout:
try:
@ -3206,7 +3202,7 @@ def open(fp, mode="r", formats=None) -> Image:
: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.
:param fp: A filename (string), os.PathLike object or a file object.
The file object must implement ``file.read``,
``file.seek``, and ``file.tell`` methods,
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
filename = ""
if isinstance(fp, Path):
filename = str(fp.resolve())
if isinstance(fp, os.PathLike):
filename = os.path.realpath(os.fspath(fp))
elif is_path(fp):
filename = fp