Python:关闭用 scipy.misc.imread 打开的文件

Python: Close a file opened with scipy.misc.imread

在 Python 中,可以像这样用 scipy 读取图像:

from scipy.misc import imread

img = imread('cats.jpg')

如何关闭打开的文件?

好的,Scipy在幕后使用PIL,PIL支持一个with块语法,当块退出时自动关闭文件:

from PIL import Image

with Image.open('test.png') as test_image:
    do_things(test_image)