用Python测试图像过滤器
我创建了一个函数,该函数根据filter_类型过滤图像,并创建一个文件。例如,如果过滤后的图像为灰度,则会在同一文件夹中生成一个名为greyscale.png的新文件 我不知道如何通过执行代码来测试,在同一文件夹中创建一个具有该名称的临时文件。这是我的密码:用Python测试图像过滤器,python,python-imaging-library,python-unittest,Python,Python Imaging Library,Python Unittest,我创建了一个函数,该函数根据filter_类型过滤图像,并创建一个文件。例如,如果过滤后的图像为灰度,则会在同一文件夹中生成一个名为greyscale.png的新文件 我不知道如何通过执行代码来测试,在同一文件夹中创建一个具有该名称的临时文件。这是我的密码: def test_image_greyscale(self): test_image = '/path/to/source/file.png/' # the image processing takes place in
def test_image_greyscale(self):
test_image = '/path/to/source/file.png/'
# the image processing takes place in the main function of the class;
result = self.filter_instance.main(
source=test_image,
filter='greyscale'
)
dirname, basename = os.path.split('greyscale.png')
file = tempfile.NamedTemporaryFile(prefix=basename, dir=dirname)
self.assertTrue(os.path.isfile(file.name))
我不明白你为什么使用
NamedTemporaryFile
。为什么不只检查os.path.isfile('greyscale.png')
或首先从test\u image
-dirname,basename=os.path.split(test\u image)
获取目录,然后创建os.path.join(dirname,'greyscale.png')
并测试此路径os.path.isfile(os.path.join(dirname,'greyscale.png'))