mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 17:54:32 +03:00
Fix handling of pathlib in save. Fixes #1460
This commit is contained in:
parent
73f3827e42
commit
d965257616
11
PIL/Image.py
11
PIL/Image.py
|
@ -1620,13 +1620,17 @@ class Image(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
filename = ""
|
filename = ""
|
||||||
|
open_fp = False
|
||||||
if isPath(fp):
|
if isPath(fp):
|
||||||
filename = fp
|
filename = fp
|
||||||
|
open_fp=True
|
||||||
elif sys.version_info >= (3, 4):
|
elif sys.version_info >= (3, 4):
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
if isinstance(fp, Path):
|
if isinstance(fp, Path):
|
||||||
filename = str(fp.resolve())
|
filename = str(fp.resolve())
|
||||||
|
open_fp=True
|
||||||
elif hasattr(fp, "name") and isPath(fp.name):
|
elif hasattr(fp, "name") and isPath(fp.name):
|
||||||
|
# only set the name for metadata purposes
|
||||||
filename = fp.name
|
filename = fp.name
|
||||||
|
|
||||||
# may mutate self!
|
# may mutate self!
|
||||||
|
@ -1655,17 +1659,14 @@ class Image(object):
|
||||||
else:
|
else:
|
||||||
save_handler = SAVE[format.upper()]
|
save_handler = SAVE[format.upper()]
|
||||||
|
|
||||||
if filename:
|
if open_fp:
|
||||||
fp = builtins.open(filename, "wb")
|
fp = builtins.open(filename, "wb")
|
||||||
close = 1
|
|
||||||
else:
|
|
||||||
close = 0
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
save_handler(self, fp, filename)
|
save_handler(self, fp, filename)
|
||||||
finally:
|
finally:
|
||||||
# do what we can to clean up
|
# do what we can to clean up
|
||||||
if close:
|
if open_fp:
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
def seek(self, frame):
|
def seek(self, frame):
|
||||||
|
|
|
@ -57,6 +57,17 @@ class TestImage(PillowTestCase):
|
||||||
self.assertEqual(im.mode, "RGB")
|
self.assertEqual(im.mode, "RGB")
|
||||||
self.assertEqual(im.size, (128, 128))
|
self.assertEqual(im.size, (128, 128))
|
||||||
|
|
||||||
|
def test_tempfile(self):
|
||||||
|
# see #1460, pathlib support breaks tempfile.TemporaryFile on py27
|
||||||
|
# Will error out on save on 3.0.0
|
||||||
|
import tempfile
|
||||||
|
im = hopper()
|
||||||
|
fp = tempfile.TemporaryFile()
|
||||||
|
im.save(fp, 'JPEG')
|
||||||
|
fp.seek(0)
|
||||||
|
reloaded = Image.open(fp)
|
||||||
|
self.assert_image_similar(im, reloaded, 20)
|
||||||
|
|
||||||
def test_internals(self):
|
def test_internals(self):
|
||||||
|
|
||||||
im = Image.new("L", (100, 100))
|
im = Image.new("L", (100, 100))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user