在 python 中对实现 ImageDraw 的方法进行单元测试

Carrying out unit testing in python on a method that implements ImageDraw

我目前正在尝试使用 pytest 模块为我正在处理的项目创建单元测试。我正在尝试测试基于一组像素绘制椭圆的 'add_point' 方法。我想要做的是检查 'draw' 以确保已成功创建椭圆。不幸的是,我不知道该怎么做,所以我们将不胜感激。到目前为止,这是我的代码:

(A) TheSlicePreviewMaker.py

import os, Image, ImageDraw, ImageFont
from json_importer import json_importer

class SlicePreviewer(object):
    def __init__(self):
        self.screen_size = (470, 470)
        self.background_colour = (86,0,255)
        self.platform_fill_colour = (100, 100, 100)
        self.platform_outline_colour = (0, 0, 0)
        self.platform_window = (0,0,469,469)
        self.point_colour = (0,0,255)
        self.config_object = json_importer("ConfigFile.txt")
        self.image = None

    def initialise_image(self):
        self.image = Image.new('RGB',self.screen_size,self.background_colour)
        draw = ImageDraw.Draw(self.image)
        draw.rectangle(self.platform_window,outline=self.platform_outline_colour,fill=self.platform_fill_colour)
        del draw

    def add_point(self, px, py):
        x1 = px - 1
        y1 = py - 1
        x2 = px + 1
        y2 = py + 1
        draw = ImageDraw.Draw(self.image)
        draw.ellipse((x1,y1,x2,y2),outline=self.point_colour,fill=self.point_colour)
        return draw #del draw

    def save_image(self, file_name):
        self.image.save(file_name, "BMP")

(B) test_TheSlicePreviewMaker.py

from TheSlicePreviewMaker import SlicePreviewer

slice_preview = SlicePreviewer()
class TestSlicePreviewer:
    def test_init(self):
        '''check that the config file object has been created on init'''
        assert slice_preview.config_object != None

    def test_initialise_image(self):
        '''verify if the image has been successfully initialised'''
        assert slice_preview.image.mode == 'RGB'

    def test_add_point(self):
        '''has the point been drawn successfully?'''
        draw = slice_preview.add_point(196,273)
        assert something

import pytest
if __name__ == '__main__':
    pytest.main("--capture=sys -v")

SN:我已经运行TheSlicePreviewMaker.py分别检查了它生成的位图文件,所以我知道代码是有效的。我想要实现的是对此进行单元测试,这样我就不必每次都去检查位图了。

一种方法是手动检查生成的图像,如果你觉得没问题,将其保存在测试旁边并使用图像差异算法(例如 ImageChops.difference)获得一个阈值,你可以用于确保未来的测试 运行 仍在绘制相同的图像。

例如:

# contents of conftest.py
from PIL import ImageChops, ImageDraw, Image
import pytest
import os
import py.path
import math
import operator

def rms_diff(im1, im2):
    """Calculate the root-mean-square difference between two images
    Taken from: http://snipplr.com/view/757/compare-two-pil-images-in-python/
    """
    h1 = im1.histogram()
    h2 = im2.histogram()

    def mean_sqr(a,b):
        if not a:
            a = 0.0
        if not b:
            b = 0.0
        return (a-b)**2

    return math.sqrt(reduce(operator.add, map(mean_sqr, h1, h2))/(im1.size[0]*im1.size[1]))


class ImageDiff:
    """Fixture used to make sure code that generates images continues to do so
    by checking the difference of the genereated image against known good versions.
    """

    def __init__(self, request):
        self.directory = py.path.local(request.node.fspath.dirname) / request.node.fspath.purebasename
        self.expected_name = (request.node.name + '.png') 
        self.expected_filename = self.directory / self.expected_name

    def check(self, im, max_threshold=0.0):
        __tracebackhide__ = True
        local = py.path.local(os.getcwd()) / self.expected_name
        if not self.expected_filename.check(file=1):
            msg = '\nExpecting image at %s, but it does not exist.\n'
            msg += '-> Generating here: %s'
            im.save(str(local))
            pytest.fail(msg % (self.expected_filename, local))
        else:
            expected = Image.open(str(self.expected_filename))
            rms_value = rms_diff(im, expected)
            if rms_value > max_threshold:
                im.save(str(local))
                msg = '\nrms_value %s > max_threshold of %s.\n'
                msg += 'Obtained image saved at %s'
                pytest.fail(msg % (rms_value, max_threshold, str(local))) 

@pytest.fixture
def image_diff(request):        
    return ImageDiff(request)

现在您可以在测试中使用 image_diff 夹具。例如:

def create_image():
    """ dummy code that generates an image, simulating some actual code """
    im = Image.new('RGB', (100, 100), (0, 0, 0))
    draw = ImageDraw.Draw(im)
    draw.ellipse((10, 10, 90, 90), outline=(0, 0, 255), 
                 fill=(255, 255, 255))
    return im      


def test_generated_image(image_diff):        
    im = create_image()
    image_diff.check(im) 

第一次 运行 此测试将失败并显示以下输出:

================================== FAILURES ===================================
____________________________ test_generated_image _____________________________

image_diff = <test_foo.ImageDiff instance at 0x029ED530>

    def test_generated_image(image_diff):
        im = create_image()
>       image_diff.check(im)
E       Failed:
E       Expecting image at X:\temp\sandbox\img-diff\test_foo\test_generated_image.png, but it does not exist.
E       -> Generating here: X:\temp\sandbox\img-diff\test_generated_image.png

然后您可以手动检查图像,如果一切正常,请将其移动到与测试文件同名的目录中,以测试名称为文件名加上“.png”扩展名。从现在开始,每当测试 运行s 时,它都会检查图像是否在可接受的范围内相似。

假设您更改代码并生成略有不同的图像,测试现在将像这样失败:

================================== FAILURES ===================================
____________________________ test_generated_image _____________________________

image_diff = <test_foo.ImageDiff instance at 0x02A4B788>

    def test_generated_image(image_diff):
        im = create_image()
>       image_diff.check(im)
E       Failed:
E       rms_value 2.52 > max_threshold of 0.0.
E       Obtained image saved at X:\temp\sandbox\img-diff\test_generated_image.png

test_foo.py:63: Failed
========================== 1 failed in 0.03 seconds ===========================

代码需要一些改进,但应该是一个好的开始。您可以找到此代码的一个版本 here.

干杯,