File: test_file_eps.py

package info (click to toggle)
pillow 8.1.2%2Bdfsg-0.3%2Bdeb11u2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 65,628 kB
  • sloc: python: 35,630; ansic: 31,009; makefile: 388; javascript: 114; sh: 77
file content (259 lines) | stat: -rw-r--r-- 8,623 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import io

import pytest

from PIL import EpsImagePlugin, Image, features

from .helper import assert_image_similar, hopper, skip_unless_feature

HAS_GHOSTSCRIPT = EpsImagePlugin.has_ghostscript()

# Our two EPS test files (they are identical except for their bounding boxes)
FILE1 = "Tests/images/zero_bb.eps"
FILE2 = "Tests/images/non_zero_bb.eps"

# Due to palletization, we'll need to convert these to RGB after load
FILE1_COMPARE = "Tests/images/zero_bb.png"
FILE1_COMPARE_SCALE2 = "Tests/images/zero_bb_scale2.png"

FILE2_COMPARE = "Tests/images/non_zero_bb.png"
FILE2_COMPARE_SCALE2 = "Tests/images/non_zero_bb_scale2.png"

# EPS test files with binary preview
FILE3 = "Tests/images/binary_preview_map.eps"


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_sanity():
    # Regular scale
    with Image.open(FILE1) as image1:
        image1.load()
        assert image1.mode == "RGB"
        assert image1.size == (460, 352)
        assert image1.format == "EPS"

    with Image.open(FILE2) as image2:
        image2.load()
        assert image2.mode == "RGB"
        assert image2.size == (360, 252)
        assert image2.format == "EPS"

    # Double scale
    with Image.open(FILE1) as image1_scale2:
        image1_scale2.load(scale=2)
        assert image1_scale2.mode == "RGB"
        assert image1_scale2.size == (920, 704)
        assert image1_scale2.format == "EPS"

    with Image.open(FILE2) as image2_scale2:
        image2_scale2.load(scale=2)
        assert image2_scale2.mode == "RGB"
        assert image2_scale2.size == (720, 504)
        assert image2_scale2.format == "EPS"


def test_invalid_file():
    invalid_file = "Tests/images/flower.jpg"

    with pytest.raises(SyntaxError):
        EpsImagePlugin.EpsImageFile(invalid_file)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_cmyk():
    with Image.open("Tests/images/pil_sample_cmyk.eps") as cmyk_image:

        assert cmyk_image.mode == "CMYK"
        assert cmyk_image.size == (100, 100)
        assert cmyk_image.format == "EPS"

        cmyk_image.load()
        assert cmyk_image.mode == "RGB"

        if features.check("jpg"):
            with Image.open("Tests/images/pil_sample_rgb.jpg") as target:
                assert_image_similar(cmyk_image, target, 10)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_showpage():
    # See https://github.com/python-pillow/Pillow/issues/2615
    with Image.open("Tests/images/reqd_showpage.eps") as plot_image:
        with Image.open("Tests/images/reqd_showpage.png") as target:
            # should not crash/hang
            plot_image.load()
            #  fonts could be slightly different
            assert_image_similar(plot_image, target, 6)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_file_object(tmp_path):
    # issue 479
    with Image.open(FILE1) as image1:
        with open(str(tmp_path / "temp.eps"), "wb") as fh:
            image1.save(fh, "EPS")


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_iobase_object(tmp_path):
    # issue 479
    with Image.open(FILE1) as image1:
        with open(str(tmp_path / "temp_iobase.eps"), "wb") as fh:
            image1.save(fh, "EPS")


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_bytesio_object():
    with open(FILE1, "rb") as f:
        img_bytes = io.BytesIO(f.read())

    with Image.open(img_bytes) as img:
        img.load()

        with Image.open(FILE1_COMPARE) as image1_scale1_compare:
            image1_scale1_compare = image1_scale1_compare.convert("RGB")
        image1_scale1_compare.load()
        assert_image_similar(img, image1_scale1_compare, 5)


def test_image_mode_not_supported(tmp_path):
    im = hopper("RGBA")
    tmpfile = str(tmp_path / "temp.eps")
    with pytest.raises(ValueError):
        im.save(tmpfile)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
@skip_unless_feature("zlib")
def test_render_scale1():
    # We need png support for these render test

    # Zero bounding box
    with Image.open(FILE1) as image1_scale1:
        image1_scale1.load()
        with Image.open(FILE1_COMPARE) as image1_scale1_compare:
            image1_scale1_compare = image1_scale1_compare.convert("RGB")
        image1_scale1_compare.load()
        assert_image_similar(image1_scale1, image1_scale1_compare, 5)

    # Non-Zero bounding box
    with Image.open(FILE2) as image2_scale1:
        image2_scale1.load()
        with Image.open(FILE2_COMPARE) as image2_scale1_compare:
            image2_scale1_compare = image2_scale1_compare.convert("RGB")
        image2_scale1_compare.load()
        assert_image_similar(image2_scale1, image2_scale1_compare, 10)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
@skip_unless_feature("zlib")
def test_render_scale2():
    # We need png support for these render test

    # Zero bounding box
    with Image.open(FILE1) as image1_scale2:
        image1_scale2.load(scale=2)
        with Image.open(FILE1_COMPARE_SCALE2) as image1_scale2_compare:
            image1_scale2_compare = image1_scale2_compare.convert("RGB")
        image1_scale2_compare.load()
        assert_image_similar(image1_scale2, image1_scale2_compare, 5)

    # Non-Zero bounding box
    with Image.open(FILE2) as image2_scale2:
        image2_scale2.load(scale=2)
        with Image.open(FILE2_COMPARE_SCALE2) as image2_scale2_compare:
            image2_scale2_compare = image2_scale2_compare.convert("RGB")
        image2_scale2_compare.load()
        assert_image_similar(image2_scale2, image2_scale2_compare, 10)


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_resize():
    files = [FILE1, FILE2, "Tests/images/illu10_preview.eps"]
    for fn in files:
        with Image.open(fn) as im:
            new_size = (100, 100)
            im = im.resize(new_size)
            assert im.size == new_size


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_thumbnail():
    # Issue #619
    # Arrange
    files = [FILE1, FILE2]
    for fn in files:
        with Image.open(FILE1) as im:
            new_size = (100, 100)
            im.thumbnail(new_size)
            assert max(im.size) == max(new_size)


def test_read_binary_preview():
    # Issue 302
    # open image with binary preview
    with Image.open(FILE3):
        pass


def test_readline(tmp_path):
    # check all the freaking line endings possible from the spec
    # test_string = u'something\r\nelse\n\rbaz\rbif\n'
    line_endings = ["\r\n", "\n", "\n\r", "\r"]
    strings = ["something", "else", "baz", "bif"]

    def _test_readline(t, ending):
        ending = "Failure with line ending: %s" % (
            "".join("%s" % ord(s) for s in ending)
        )
        assert t.readline().strip("\r\n") == "something", ending
        assert t.readline().strip("\r\n") == "else", ending
        assert t.readline().strip("\r\n") == "baz", ending
        assert t.readline().strip("\r\n") == "bif", ending

    def _test_readline_io_psfile(test_string, ending):
        f = io.BytesIO(test_string.encode("latin-1"))
        t = EpsImagePlugin.PSFile(f)
        _test_readline(t, ending)

    def _test_readline_file_psfile(test_string, ending):
        f = str(tmp_path / "temp.txt")
        with open(f, "wb") as w:
            w.write(test_string.encode("latin-1"))

        with open(f, "rb") as r:
            t = EpsImagePlugin.PSFile(r)
            _test_readline(t, ending)

    for ending in line_endings:
        s = ending.join(strings)
        _test_readline_io_psfile(s, ending)
        _test_readline_file_psfile(s, ending)


def test_open_eps():
    # https://github.com/python-pillow/Pillow/issues/1104
    # Arrange
    FILES = [
        "Tests/images/illu10_no_preview.eps",
        "Tests/images/illu10_preview.eps",
        "Tests/images/illuCS6_no_preview.eps",
        "Tests/images/illuCS6_preview.eps",
    ]

    # Act / Assert
    for filename in FILES:
        with Image.open(filename) as img:
            assert img.mode == "RGB"


@pytest.mark.skipif(not HAS_GHOSTSCRIPT, reason="Ghostscript not available")
def test_emptyline():
    # Test file includes an empty line in the header data
    emptyline_file = "Tests/images/zero_bb_emptyline.eps"

    with Image.open(emptyline_file) as image:
        image.load()
    assert image.mode == "RGB"
    assert image.size == (460, 352)
    assert image.format == "EPS"