aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_task_scale_images.py
blob: 46a39a4abc0072afd7333f919ea62e6f97c24a4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import os
from tempfile import NamedTemporaryFile

import pytest
from PIL import Image, ImageDraw

from nikola.plugins.task import scale_images

# These tests don't require valid profiles. They need only to verify
# that profile data is/isn't saved with images.
# It would be nice to use PIL.ImageCms to create valid profiles, but
# in many Pillow distributions ImageCms is a stub.
# ICC file data format specification:
# http://www.color.org/icc32.pdf
PROFILE = b"invalid profile data"


def test_handling_icc_profiles(test_images, destination_dir):
    filename, expected_profile = test_images

    pathname = os.path.join(str(destination_dir), filename)
    assert os.path.exists(pathname), pathname

    img = Image.open(pathname)
    actual_profile = img.info.get("icc_profile")
    assert actual_profile == expected_profile


@pytest.fixture(
    params=[
        pytest.param(True, id="with icc filename"),
        pytest.param(False, id="without icc filename"),
    ]
)
def test_images(request, preserve_icc_profiles, source_dir, site):
    image_filename = create_src_image(str(source_dir), request.param)
    run_task(site)

    if request.param:
        yield image_filename, PROFILE if preserve_icc_profiles else None
    else:
        yield image_filename, None


@pytest.fixture(
    params=[
        pytest.param(True, id="profiles preserved"),
        pytest.param(False, id="profiles not preserved"),
    ]
)
def preserve_icc_profiles(request):
    return request.param


@pytest.fixture
def source_dir(tmpdir_factory):
    return tmpdir_factory.mktemp("image_source")


@pytest.fixture
def site(preserve_icc_profiles, source_dir, destination_dir):
    config = {
        "IMAGE_FOLDERS": {str(source_dir): ""},
        "OUTPUT_FOLDER": str(destination_dir),
        "IMAGE_THUMBNAIL_SIZE": 128,
        "IMAGE_THUMBNAIL_FORMAT": "{name}.thumbnail{ext}",
        "MAX_IMAGE_SIZE": 512,
        "FILTERS": {},
        "PRESERVE_EXIF_DATA": False,
        "EXIF_WHITELIST": {},
        "PRESERVE_ICC_PROFILES": preserve_icc_profiles,
    }
    return FakeSite(config)


class FakeSite:
    def __init__(self, config):
        self.config = config
        self.debug = True


@pytest.fixture
def destination_dir(tmpdir_factory):
    return tmpdir_factory.mktemp("image_output")


def run_task(site):
    task_instance = get_task_instance(site)
    for task in task_instance.gen_tasks():
        for action, args in task.get("actions", []):
            action(*args)


def get_task_instance(site):
    result = scale_images.ScaleImage()
    result.set_site(site)
    return result


def create_src_image(testdir, use_icc_profile):
    img = create_test_image()
    pathname = tmp_img_name(testdir)

    # Test two variants: with and without an associated icc_profile
    if use_icc_profile:
        img.save(pathname, icc_profile=PROFILE)
    else:
        img.save(pathname)

    return os.path.basename(pathname)


def create_test_image():
    # Make a white image with a red stripe on the diagonal.
    width = 64
    height = 64
    img = Image.new("RGB", (width, height), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    draw.line((0, 0, width, height), fill=(255, 128, 128))
    draw.line((width, 0, 0, height), fill=(128, 128, 255))
    return img


def tmp_img_name(dirname):
    pathname = NamedTemporaryFile(suffix=".jpg", dir=dirname, delete=False)
    return pathname.name