| 1 | """Functions to facilitate direct comparison of PIL.Image instances""" |
| 2 | from typing import List, Optional, Tuple |
| 3 | |
| 4 | from PIL.Image import Image |
| 5 | |
| 6 | from pixelmatch import core |
| 7 | from pixelmatch.types import ImageSequence, MutableImageSequence, RGBTuple |
| 8 | |
| 9 | |
| 10 | def pixelmatch( |
| 11 | img1: Image, |
| 12 | img2: Image, |
| 13 | output: Optional[Image] = None, |
| 14 | threshold: float = 0.1, |
| 15 | includeAA: bool = False, |
| 16 | alpha: float = 0.1, |
| 17 | aa_color: RGBTuple = (255, 255, 0), |
| 18 | diff_color: RGBTuple = (255, 0, 0), |
| 19 | diff_mask: bool = False, |
| 20 | fail_fast: bool = False, |
| 21 | ) -> int: |
| 22 | """ |
| 23 | Compares two images, writes the output diff and returns the number of mismatched pixels. |
| 24 | Serves the same purpose as pixelmatch.pixelmatch, but takes PIL.Images as input instead |
| 25 | of raw image data. |
| 26 | |
| 27 | :param img1: PIL.Image data to compare with img2. Must be the same size as img2 |
| 28 | :param img2: PIL.Image data to compare with img2. Must be the same size as img1 |
| 29 | :param output: Image data to write the diff to. Should be the same size as |
| 30 | :param threshold: matching threshold (0 to 1); smaller is more sensitive, defaults to 1 |
| 31 | :param includeAA: whether or not to skip anti-aliasing detection, ie if includeAA is True, |
| 32 | detecting and ignoring anti-aliased pixels is disabled. Defaults to False |
| 33 | :param alpha: opacity of original image in diff output, defaults to 0.1 |
| 34 | :param aa_color: tuple of RGB color of anti-aliased pixels in diff output, |
| 35 | defaults to (255, 255, 0) (yellow) |
| 36 | :param diff_color: tuple of RGB color of the color of different pixels in diff output, |
| 37 | defaults to (255, 0, 0) (red) |
| 38 | :param diff_mask: whether or not to draw the diff over a transparent background (a mask), |
| 39 | defaults to False |
| 40 | :param fail_fast: if true, will return after first different pixel. Defaults to false |
| 41 | :return: number of pixels that are different or 1 if fail_fast == true |
| 42 | """ |
| 43 | width, height = img1.size |
| 44 | img1 = from_PIL_to_raw_data(img1) |
| 45 | img2 = from_PIL_to_raw_data(img2) |
| 46 | |
| 47 | if output is not None: |
| 48 | raw_output: Optional[MutableImageSequence] = from_PIL_to_raw_data(output) |
| 49 | else: |
| 50 | raw_output = None |
| 51 | |
| 52 | diff_pixels = core.pixelmatch( |
| 53 | img1, |
| 54 | img2, |
| 55 | width, |
| 56 | height, |
| 57 | raw_output, |
| 58 | threshold=threshold, |
| 59 | includeAA=includeAA, |
| 60 | alpha=alpha, |
| 61 | aa_color=aa_color, |
| 62 | diff_color=diff_color, |
| 63 | diff_mask=diff_mask, |
| 64 | fail_fast=fail_fast, |
| 65 | ) |
| 66 | |
| 67 | if raw_output is not None and output is not None: |
| 68 | output.putdata(to_PIL_from_raw_data(raw_output)) |
| 69 | |
| 70 | return diff_pixels |
| 71 | |
| 72 | |
| 73 | def from_PIL_to_raw_data(pil_img: Image) -> MutableImageSequence: |
| 74 | """ |
| 75 | Converts a PIL.Image object from [(R1, B1, A1, A1), (R2, ...), ...] to our raw data format |
| 76 | [R1, G1, B1, A1, R2, ...]. |
| 77 | |
| 78 | :param pil_img: |
| 79 | :return: |
| 80 | """ |
| 81 | return [item for sublist in pil_img.convert("RGBA").getdata() for item in sublist] |
| 82 | |
| 83 | |
| 84 | def to_PIL_from_raw_data( |
| 85 | raw_data: ImageSequence, |
| 86 | ) -> List[Tuple[float, float, float, float]]: |
| 87 | """ |
| 88 | Converts from the internal raw data format of [R1, G1, B1, A1, R2, ...] to PIL's raw data format, ie |
| 89 | [(R1, B1, A1, A1), (R2, ...), ...] |
| 90 | :return: raw image data in a PIL appropriate format |
| 91 | """ |
| 92 | return [*zip(raw_data[::4], raw_data[1::4], raw_data[2::4], raw_data[3::4])] |