Command line interface positive tests.
| 236 | |
| 237 | |
| 238 | class CLIPTest(CLITestBase): |
| 239 | ''' |
| 240 | Command line interface positive tests. |
| 241 | ''' |
| 242 | # pylint: disable=too-many-public-methods |
| 243 | |
| 244 | def compare(self, image1: Path, image2: Path) -> bool: |
| 245 | ''' |
| 246 | Utility function to compare two images. |
| 247 | |
| 248 | Note that this comparison tests only decoded color values; file |
| 249 | metadata is ignored, and encoding methods are not compared. |
| 250 | |
| 251 | Args: |
| 252 | image1: Path to the first image. |
| 253 | image2: Path to the second image. |
| 254 | |
| 255 | Return: |
| 256 | True if the images are the same, False otherwise. |
| 257 | ''' |
| 258 | # Use Any here because PIL interface doesn't have clean types |
| 259 | img1: Any = Image.open(image1) |
| 260 | img2: Any = Image.open(image2) |
| 261 | |
| 262 | # Images must have same size |
| 263 | if img1.size != img2.size: |
| 264 | return False |
| 265 | |
| 266 | # Images must have same number of color channels |
| 267 | if img1.getbands() != img2.getbands(): |
| 268 | # ... unless the only different is alpha |
| 269 | self.assertEqual(img1.getbands(), ('R', 'G', 'B')) |
| 270 | self.assertEqual(img2.getbands(), ('R', 'G', 'B', 'A')) |
| 271 | |
| 272 | # ... and the alpha is always one |
| 273 | bands = img2.split() |
| 274 | alpha_hist = bands[3].histogram() |
| 275 | self.assertEqual(sum(alpha_hist[:-1]), 0) |
| 276 | |
| 277 | # Generate a version of img2 without alpha |
| 278 | img2 = Image.merge('RGB', (bands[0], bands[1], bands[2])) |
| 279 | |
| 280 | # Compute sum of absolute differences |
| 281 | dat1 = numpy.array(img1) |
| 282 | dat2 = numpy.array(img2) |
| 283 | sad = numpy.sum(numpy.abs(dat1 - dat2)) |
| 284 | return sad == 0 |
| 285 | |
| 286 | def get_channel_rmse(self, image1: Path, image2: Path) -> list[float]: |
| 287 | ''' |
| 288 | Get the channel-by-channel root mean square error. |
| 289 | |
| 290 | Images must have same channel count. |
| 291 | |
| 292 | Args: |
| 293 | image1: Path to the first image. |
| 294 | image2: Path to the second image. |
| 295 |
nothing calls this directly
no outgoing calls
no test coverage detected