mardi 28 avril 2020

Images generated from the same array are different during array comparision

In this code sample, the assertion in the function fails.

from pathlib import Path

import numpy as np
import PIL.Image


def make_images(tmp_path):
    np.random.seed(0)
    shape = (4, 6, 3)
    rgb = np.random.randint(0, 256, shape, dtype=np.uint8)
    test_image = PIL.Image.fromarray(rgb)
    image_path = tmp_path / 'test_image.jpg'
    test_image.save(image_path)
    return image_path, rgb


def test_Image_load_rgb(tmp_path):
    image_path, original_rgb = make_images(tmp_path)
    rgb2 = np.array(PIL.Image.open(image_path))
    assert np.array_equal(rgb2, original_rgb)


if __name__ == '__main__':
    test_Image_load_rgb(tmp_path)

When I look at the two arrays, original_rgb and rgb2, they have different values, so of course it is failing, but I don't understand why their arrays have different values.

Opening them both as images using PIL.Image.fromarray(), visually they look similar but not the same, the brightness values are slightly altered, visually.

I don't understand why this is.

The two images are:
test_image.jpg test_image2.jpg

Note: This is fails the same way for both pytest and when run as a script.

Aucun commentaire:

Enregistrer un commentaire