mirror of
https://github.com/wassname/scikit-image.git
synced 2026-08-05 13:21:12 +08:00
Add temporary_file context manager
This provides writeable filenames that are cleaned up when the context is exited.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
from tempfile import NamedTemporaryFile
|
||||
from contextlib import contextmanager
|
||||
import os
|
||||
|
||||
@contextmanager
|
||||
def temporary_file(suffix=''):
|
||||
"""Yield a writeable temporary filename that is deleted on context exit.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
suffix : string, optional
|
||||
The suffix for the file.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> import numpy as np
|
||||
>>> from skimage import io
|
||||
>>> with temporary_file('.tif') as tempfile:
|
||||
... im = np.zeros((5, 5), np.uint8)
|
||||
... io.imsave(tempfile, im)
|
||||
... assert np.all(io.imread(tempfile) == im)
|
||||
"""
|
||||
tempfile_stream = NamedTemporaryFile(suffix=suffix, delete=False)
|
||||
tempfile = tempfile_stream.name
|
||||
tempfile_stream.close()
|
||||
yield tempfile
|
||||
os.remove(tempfile)
|
||||
@@ -11,6 +11,7 @@ from .. import (imread, imsave, use_plugin, reset_plugins,
|
||||
Image as ioImage)
|
||||
from ..._shared.testing import mono_check, color_check
|
||||
from ..._shared._warnings import expected_warnings
|
||||
from ..._shared._tempfile import temporary_file
|
||||
|
||||
from six import BytesIO
|
||||
|
||||
@@ -99,10 +100,9 @@ def test_imread_truncated_jpg():
|
||||
|
||||
def test_jpg_quality_arg():
|
||||
chessboard = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy'))
|
||||
with NamedTemporaryFile(suffix='.jpg') as jpg:
|
||||
fname = jpg.name
|
||||
imsave(fname, chessboard, quality=95)
|
||||
im = imread(fname)
|
||||
with temporary_file(suffix='.jpg') as jpg:
|
||||
imsave(jpg, chessboard, quality=95)
|
||||
im = imread(jpg)
|
||||
sim = ssim(chessboard, im,
|
||||
dynamic_range=chessboard.max() - chessboard.min())
|
||||
assert sim > 0.99
|
||||
@@ -117,12 +117,10 @@ def test_imread_uint16_big_endian():
|
||||
|
||||
class TestSave:
|
||||
def roundtrip_file(self, x):
|
||||
f = NamedTemporaryFile(suffix='.png')
|
||||
fname = f.name
|
||||
f.close()
|
||||
imsave(fname, x)
|
||||
y = imread(fname)
|
||||
return y
|
||||
with temporary_file(suffix='.png') as fname:
|
||||
imsave(fname, x)
|
||||
y = imread(fname)
|
||||
return y
|
||||
|
||||
def roundtrip_pil_image(self, x):
|
||||
pil_image = ndarray_to_pil(x)
|
||||
@@ -226,20 +224,18 @@ def test_cmyk():
|
||||
|
||||
class TestSaveTIF:
|
||||
def roundtrip(self, dtype, x, compress):
|
||||
f = NamedTemporaryFile(suffix='.tif')
|
||||
fname = f.name
|
||||
f.close()
|
||||
if dtype == np.bool:
|
||||
expected = ['low contrast']
|
||||
else:
|
||||
expected = []
|
||||
with expected_warnings(expected):
|
||||
if compress > 0:
|
||||
imsave(fname, x, compress=compress)
|
||||
with temporary_file(suffix='.tif') as fname:
|
||||
if dtype == np.bool:
|
||||
expected = ['low contrast']
|
||||
else:
|
||||
imsave(fname, x)
|
||||
y = imread(fname)
|
||||
assert_array_equal(x, y)
|
||||
expected = []
|
||||
with expected_warnings(expected):
|
||||
if compress > 0:
|
||||
imsave(fname, x, compress=compress)
|
||||
else:
|
||||
imsave(fname, x)
|
||||
y = imread(fname)
|
||||
assert_array_equal(x, y)
|
||||
|
||||
def test_imsave_roundtrip(self):
|
||||
for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]:
|
||||
|
||||
Reference in New Issue
Block a user