예제 #1
0
def assert_pil_images_equal(a, b, epsilon=0):
    abytes = a.convert('RGBA').tobytes()
    bbytes = b.convert('RGBA').tobytes()
    if abytes != bbytes:
        from tmxlib_test.image_to_term import image_to_term256
        from PIL import ImageChops, ImageOps
        print("Expected: ({im.size[0]}x{im.size[1]})".format(im=a))
        print(image_to_term256(a))
        print("Got: ({im.size[0]}x{im.size[1]})".format(im=b))
        print(image_to_term256(b))

        diff = ImageChops.difference(a, b).convert('RGB')
        diff = ImageOps.autocontrast(diff)
        print('Difference:')
        print(image_to_term256(diff))

        assert len(abytes) == len(bbytes), 'unequal image size'

        max_pixel_delta = 0
        zero_alphas = 0
        try:
            Counter = collections.Counter
        except AttributeError:  # pragma: no cover -- Python 2.6
            counters = None
        else:
            counters = [Counter() for i in range(4)]
        for bands in grouper(zip(abytes, bbytes), 4):
            alpha_a, alpha_b = bands[-1]
            if alpha_a == alpha_b == b'\0':
                zero_alphas += 1
                continue
            for band_index, (ba, bb) in enumerate(bands):
                pixel_delta = ord_(ba) - ord_(bb)
                max_pixel_delta = max(abs(pixel_delta), max_pixel_delta)
                if counters:
                    counters[band_index][pixel_delta] += 1

        print('Ignored pixels (zero alpha):', zero_alphas)
        if counters:
            print("Pixel deltas:")
            for band_index, counter in enumerate(counters):
                print('  {0}:'.format('RGBA'[band_index]))
                for delta, count in sorted(counter.items()):
                    print('   {0:4}: {1}x'.format(delta, count))

        print('Max |pixel delta|:', max_pixel_delta)
        assert max_pixel_delta <= epsilon
예제 #2
0
def assert_pil_images_equal(a, b, epsilon=0):
    abytes = a.convert('RGBA').tobytes()
    bbytes = b.convert('RGBA').tobytes()
    if abytes != bbytes:
        from tmxlib_test.image_to_term import image_to_term256
        from PIL import ImageChops, ImageOps
        print("Expected: ({im.size[0]}x{im.size[1]})".format(im=a))
        print(image_to_term256(a))
        print("Got: ({im.size[0]}x{im.size[1]})".format(im=b))
        print(image_to_term256(b))

        diff = ImageChops.difference(a, b).convert('RGB')
        diff = ImageOps.autocontrast(diff)
        print('Difference:')
        print(image_to_term256(diff))

        assert len(abytes) == len(bbytes), 'unequal image size'

        max_pixel_delta = 0
        zero_alphas = 0
        try:
            Counter = collections.Counter
        except AttributeError:  # pragma: no cover -- Python 2.6
            counters = None
        else:
            counters = [Counter() for i in range(4)]
        for bands in grouper(zip(abytes, bbytes), 4):
            alpha_a, alpha_b = bands[-1]
            if alpha_a == alpha_b == b'\0':
                zero_alphas += 1
                continue
            for band_index, (ba, bb) in enumerate(bands):
                pixel_delta = ord_(ba) - ord_(bb)
                max_pixel_delta = max(abs(pixel_delta), max_pixel_delta)
                if counters:
                    counters[band_index][pixel_delta] += 1

        print('Ignored pixels (zero alpha):', zero_alphas)
        if counters:
            print("Pixel deltas:")
            for band_index, counter in enumerate(counters):
                print('  {0}:'.format('RGBA'[band_index]))
                for delta, count in sorted(counter.items()):
                    print('   {0:4}: {1}x'.format(delta, count))

        print('Max |pixel delta|:', max_pixel_delta)
        assert max_pixel_delta <= epsilon
예제 #3
0
def assert_png_repr_equal(image, filename, epsilon=0):
    data = image._repr_png_()
    a = pil_image_open(get_test_filename(filename))
    b = pil_image_open(BytesIO(data))
    assert b.format == 'PNG'
    abytes = a.convert('RGBA').tobytes()
    bbytes = b.convert('RGBA').tobytes()
    if abytes != bbytes:
        from tmxlib_test.image_to_term import image_to_term256
        from PIL import ImageChops, ImageOps
        print("Expected: ({im.size[0]}x{im.size[1]})".format(im=a))
        print(image_to_term256(a))
        print("Got: ({im.size[0]}x{im.size[1]})".format(im=b))
        print(image_to_term256(b))

        diff = ImageChops.difference(a, b).convert('RGB')
        diff = ImageOps.autocontrast(diff)
        print('Difference:')
        print(image_to_term256(diff))

        assert len(abytes) == len(bbytes), 'unequal image size'

        max_pixel_delta = 0
        try:
            Counter = collections.Counter
        except AttributeError:  # pragma: no cover -- Python 2.6
            counters = None
        else:
            counters = [Counter() for i in range(4)]
        for i, (ba, bb) in enumerate(zip(abytes, bbytes)):
            pixel_delta = ord_(ba) - ord_(bb)
            max_pixel_delta = max(abs(pixel_delta), max_pixel_delta)
            if counters:
                counters[i % 4][pixel_delta] += 1

        if counters:
            print("Pixel deltas:")
            for band_index, counter in enumerate(counters):
                print('  {0}:'.format('RGBA'[band_index]))
                for delta, count in sorted(counter.items()):
                    print('   {0:4}: {1}x'.format(delta, count))

        print('Max |pixel delta|:', max_pixel_delta)
        assert max_pixel_delta <= epsilon