简体   繁体   English

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

[英]Python: Close a file opened with scipy.misc.imread

In Python one can read in an image with scipy like so: 在Python中,可以像下面这样用scipy读取图像:

from scipy.misc import imread

img = imread('cats.jpg')

How can one close that opened file? 如何关闭打开的文件?

Okay, Scipy uses PIL under the cover, and PIL supports a with block syntax that automatically closes the file when the block is exited: 好的,Scipy在后台使用PIL,并且PIL支持with块语法,该语法在退出块时自动关闭文件:

from PIL import Image

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM