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
def image_data(self): try: return self._image_data except AttributeError: self.load_image() data = self._image_data_original if self.trans: xtrans = tuple(int(n * 255) for n in self.trans[:3]) new_data = [] for line in data: new_data.append(array( 'B', itertools.chain.from_iterable( v[:3] + (0,) if tuple(v[:3]) == xtrans else v for v in grouper(line, 4)))) self._image_data = new_data else: self._image_data = data return self._image_data
def image_data(self): try: return self._image_data except AttributeError: self.load_image() data = self._image_data_original if self.trans: xtrans = tuple(int(n * 255) for n in self.trans[:3]) new_data = [] for line in data: new_data.append( array( 'B', itertools.chain.from_iterable( v[:3] + (0, ) if tuple(v[:3]) == xtrans else v for v in grouper(line, 4)))) self._image_data = new_data else: self._image_data = data return self._image_data