【发布时间】:2016-07-23 12:21:53
【问题描述】:
当我在 Python3 上使用 Pillow(版本 3.3.0,通过 pip 安装)将图像数据加载到 numpy 数组中时,我的单元测试报告了 ResourceWarning。例如,当我运行以下脚本时会出现警告:
#! /usr/bin/env python3
import unittest
import numpy as np
import PIL.Image
def load_image():
with PIL.Image.open('test.tif') as im:
return np.array(im)
class TestData(unittest.TestCase):
def test_PIL(self):
im = load_image()
print(im.shape)
unittest.main()
输出是
./err.py:14: ResourceWarning: unclosed file <_io.BufferedReader name='test.tif'>
im = load_image()
(420, 580)
.
----------------------------------------------------------------------
Ran 1 test in 0.012s
OK
(如果我不将图像包装在 numpy 数组中,警告就会消失。)
此资源警告是否表明我的代码中有泄漏(例如,除了使用 with 语句之外,我是否需要以某种方式“关闭”图像文件)?或者,如果警告是虚假的,我该如何禁用它?
【问题讨论】:
-
如果你使用
np.array(im.getdata())会怎样? -
@WayneWerner using
np.array(im.getdata())我仍然收到警告,而且数组的形状也错误。
标签: python-3.x numpy pillow