Ejemplo n.º 1
0
def test_io(subtests, test_image_file):
    for backend1, backend2 in itertools.combinations(
            pyimagetest.image_backends.values(), 2):
        with subtests.test(backend1=backend1, backend2=backend2):
            image1 = backend1.import_image(test_image_file)
            image2 = backend2.import_image(test_image_file)
            pyimagetest.assert_images_almost_equal(image1, image2)
Ejemplo n.º 2
0
def test_BottomRightCrop(test_image):
    size = 200

    transform = transforms.BottomRightCrop(size)
    actual = transform(test_image)
    desired = test_image[:, :, -size:, -size:]
    pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 3
0
def test_TopLeftCrop(test_image):
    size = 200

    transform = transforms.TopLeftCrop(size)
    actual = transform(test_image)
    desired = test_image[:, :, :size, :size]
    pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 4
0
def test_Crop(subtests, test_image):
    origin = (200, 300)
    size = (50, 30)

    spatial_slices = {
        ("top", "left"): [
            slice(origin[0], origin[0] + size[0]),
            slice(origin[1], origin[1] + size[1]),
        ],
        ("bottom", "left"): [
            slice(origin[0] - size[0], origin[0]),
            slice(origin[1], origin[1] + size[1]),
        ],
        ("top", "right"): [
            slice(origin[0], origin[0] + size[0]),
            slice(origin[1] - size[1], origin[1]),
        ],
        ("bottom", "right"): [
            slice(origin[0] - size[0], origin[0]),
            slice(origin[1] - size[1], origin[1]),
        ],
    }

    for (vert_anchor, horz_anchor), spatial_slice in spatial_slices.items():
        with subtests.test(vert_anchor=vert_anchor, horz_anchor=horz_anchor):
            transform = transforms.Crop(origin,
                                        size,
                                        vert_anchor=vert_anchor,
                                        horz_anchor=horz_anchor)
            actual = transform(test_image)
            desired = test_image[(slice(None), slice(None), *spatial_slice)]
            pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 5
0
def test_CenterCrop():
    image = torch.rand(1, 1, 100, 100)
    size = 50

    transform = transforms.CenterCrop(size)
    actual = transform(image)
    desired = image[:, :, size // 2:-size // 2, size // 2:-size // 2]
    pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 6
0
def test_write_image(tmpdir):
    torch.manual_seed(0)
    image = torch.rand(3, 100, 100)

    file = path.join(tmpdir, "tmp_image.png")
    image_.write_image(image, file)

    actual = image_.read_image(file=file)

    desired = image
    pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 7
0
def test_read_image_resize_scalar(test_image_file, test_image_pil):
    edge_size = 200

    aspect_ratio = image_.calculate_aspect_ratio(
        (test_image_pil.height, test_image_pil.width)
    )
    image_size = image_.edge_to_image_size(edge_size, aspect_ratio)

    actual = image_.read_image(test_image_file, size=edge_size)
    desired = test_image_pil.resize(image_size[::-1])
    pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 8
0
def test_ExportToPIL_multi_image(test_image, test_image_pil):
    batch_size = 2
    export_transform = transforms.ExportToPIL()

    batched_image = test_image.repeat(2, 1, 1, 1)
    actuals = export_transform(batched_image)
    desired = test_image_pil

    assert isinstance(actuals, tuple)
    assert len(actuals) == batch_size

    for actual in actuals:
        pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 9
0
def test_ValidRandomCrop():
    def randint(range):
        return torch.randint(range + 1, ()).item()

    image_size = (100, 100)
    crop_size = (10, 20)
    image = torch.rand(1, 1, *image_size)

    image_height, image_width = image_size
    crop_height, crop_width = crop_size
    torch.manual_seed(0)
    vert_origin = randint(image_height - crop_height)
    horz_origin = randint(image_width - crop_width)

    torch.manual_seed(0)
    transform = transforms.ValidRandomCrop(crop_size)

    actual = transform(image)
    desired = image[:, :, vert_origin:vert_origin + crop_height,
                    horz_origin:horz_origin + crop_width, ]
    pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 10
0
def test_ImportFromPIL_single_image(test_image, test_image_pil):
    import_transform = transforms.ImportFromPIL(make_batched=False)

    actual = import_transform(test_image_pil)
    desired = image.make_single_image(test_image)
    pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 11
0
 def assert_transform_equality(pystiche_image, pil_image):
     actual = pystiche_transform(pystiche_image)
     desired = pil_transform(pil_image)
     pyimagetest.assert_images_almost_equal(actual, desired, mae=mae)
Ejemplo n.º 12
0
def assert_is_identity_transform(transform, image=None, mae=1e-2):
    if image is None:
        image = read_image()
    pyimagetest.assert_images_almost_equal(image, transform(image), mae=mae)
Ejemplo n.º 13
0
def test_read_image_resize(test_image_file, test_image_pil):
    image_size = (200, 300)
    actual = image_.read_image(test_image_file, size=image_size)
    desired = test_image_pil.resize(image_size[::-1])
    pyimagetest.assert_images_almost_equal(actual, desired)
Ejemplo n.º 14
0
def test_read_image(test_image_file, test_image):
    actual = image_.read_image(test_image_file)
    desired = test_image
    assert image_.is_batched_image(actual)
    pyimagetest.assert_images_almost_equal(actual, desired)