Coverage for gws-app/gws/lib/image/_test.py: 0%
139 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-17 01:37 +0200
1"""Tests for the image module."""
2import os
4import gws
5import gws.test.util as u
6import gws.lib.image as image
8import PIL.Image
9import PIL.ImageDraw
10import PIL.ImageFont
13# creates the images to compare with using PIL
14def test_create_images():
15 red = PIL.Image.new('RGBA', (50, 50), (255, 0, 0, 255))
16 red.save('/tmp/red.png')
18 red_crop = red.crop((10, 10, 30, 30))
19 red_crop.save('/tmp/red-crop.png')
21 red_box = PIL.Image.new('RGBA', (50, 50), (255, 0, 0, 255))
22 draw = PIL.ImageDraw.Draw(red_box)
23 draw.rectangle((0, 0, 49, 49), outline=(0, 255, 0, 255))
24 red_box.save('/tmp/red-box.png')
26 x = PIL.Image.new('RGBA', (25, 25), (255, 255, 255, 255))
27 draw = PIL.ImageDraw.Draw(x)
28 draw.line((0, 12, 24, 12), fill=(0, 255, 0, 255))
29 draw.line((12, 0, 12, 24), fill=(0, 255, 0, 255))
30 x.save('/tmp/x.png')
32 red_paste = PIL.Image.new('RGBA', (50, 50), (255, 0, 0, 255))
33 red_paste.paste(x, (12, 12))
34 red_paste.save('/tmp/red-paste.png')
36 red_rotate = red.rotate(-15)
37 red_rotate.save('/tmp/red-rotate.png')
39 red_text = PIL.Image.new('RGBA', (50, 50), (255, 0, 0, 255))
40 draw = PIL.ImageDraw.Draw(red_text)
41 font = PIL.ImageFont.load_default()
42 draw.multiline_text((10, 10), 'FOOBAR', font=font, fill=(0, 255, 0, 255))
43 red_text.save('/tmp/red-text.png')
45 red_compose_size_difference = PIL.Image.new('RGBA', (50, 50), (255, 0, 0, 255))
46 x = x.resize((50, 50), resample=PIL.Image.BICUBIC)
47 red_compose_size_difference.alpha_composite(x)
48 red_compose_size_difference.save('/tmp/red-compose-size-diff.png')
50 red_compose_float = PIL.Image.new('RGBA', (50, 50), (255, 0, 0, 255))
51 x.putalpha(int(255 * 0.5))
52 red_compose_float = PIL.Image.alpha_composite(red_compose_float, x)
53 red_compose_float.save('/tmp/red-compose-float.png')
55 blue = PIL.Image.new('RGBA', (50, 50), (0, 0, 255, 255))
56 blue.save('/tmp/blue.png')
57 red_compose = PIL.Image.alpha_composite(red, blue)
58 red_compose.save('/tmp/red-compose.png')
60 assert True
63# https://i-converter.com/files/png-to-url used to get the url
64red = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9TpaItCnYQcchQnSyIFXHUKhShQqgVWnUwufQLmjQkKS6OgmvBwY/FqoOLs64OroIg+AHi7OCk6CIl/q8ptIjx4Lgf7+497t4BQr3MNKtrAtB020wl4mImuyoGXiFgAH2IISQzy5iTpCQ8x9c9fHy9i/Is73N/jpCasxjgE4lnmWHaxBvE05u2wXmfOMyKskp8Tjxu0gWJH7muuPzGudBkgWeGzXRqnjhMLBY6WOlgVjQ14iniiKrplC9kXFY5b3HWylXWuid/YTCnryxzneYIEljEEiSIUFBFCWXYiNKqk2IhRftxD/9w0y+RSyFXCYwcC6hAg9z0g//B726tfGzSTQrGge4Xx/kYBQK7QKPmON/HjtM4AfzPwJXe9lfqwMwn6bW2FjkC+reBi+u2puwBlzvA0JMhm3JT8tMU8nng/Yy+KQsM3gK9a25vrX2cPgBp6ip5AxwcAmMFyl73eHdPZ2//nmn19wNcpnKe/XiMrwAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+gCEAwaAksjVSMAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAARElEQVRo3u3PMREAAAgEoNf+nbWFgwcNqEkmD3SeEBEREREREREREREREREREREREREREREREREREREREREREREREbmxB7ECYhgv8rEAAAAASUVORK5CYII='
67def test_from_size():
68 img = image.from_size((50, 50), color=(255, 0, 0, 255))
69 img2 = image.from_path('/tmp/red.png')
70 assert img.compare_to(img2) == 0
73def test_from_bytes():
74 file = open('/tmp/red.png', 'rb')
75 byt = file.read()
76 img = image.from_bytes(byt)
77 img2 = image.from_path('/tmp/red.png')
78 assert img.compare_to(img2) == 0
81def test_from_raw_data():
82 img = image.from_data_url(red)
83 arr = img.to_array()
84 arr = arr.tobytes()
85 img2 = image.from_raw_data(arr, 'RGBA', (50, 50))
86 assert img.compare_to(img2) == 0
89def test_from_path():
90 img = image.from_path('/tmp/red.png')
91 img2 = image.from_data_url(red)
92 assert img.compare_to(img2) == 0
95# https://i-converter.com/files/png-to-url used to get the url
96def test_from_data_url():
97 img = image.from_data_url(red)
98 img2 = image.from_path('/tmp/red.png')
99 assert img.compare_to(img2) == 0
102def test_from_data_url_exception():
103 with u.raises(Exception, match=f'invalid data url'):
104 img = image.from_data_url('foobar')
107# not implemented
108def test_from_svg():
109 with u.raises(Exception):
110 img = image.from_svg('test', (50, 50))
113def test_size():
114 img = image.from_path('/tmp/red.png')
115 assert img.size() == (50, 50)
118def test_resize():
119 img = image.from_size((100, 100))
120 img.resize((50, 50))
121 assert img.size() == (50, 50)
124def test_rotate():
125 img = image.from_data_url(red)
126 img = img.rotate(-15)
127 img2 = image.from_path('/tmp/red-rotate.png')
128 # sometimes not exactly 100% similar due to antialiasing when rotating
129 assert img.compare_to(img2) == 0
132def test_crop():
133 img = image.from_data_url(red)
134 img = img.crop((10, 10, 30, 30))
135 img2 = image.from_path('/tmp/red-crop.png')
136 assert img.compare_to(img2) == 0
139def test_paste():
140 img = image.from_data_url(red)
141 x = image.from_path('/tmp/x.png')
142 img = img.paste(x, where=(12, 12))
143 img2 = image.from_path('/tmp/red-paste.png')
144 assert img.compare_to(img2) == 0
147# not right
148def test_compose_float():
149 img = image.from_data_url(red)
150 img2 = image.from_path('/tmp/x.png')
151 img = img.compose(img2, opacity=0.5)
152 img3 = image.from_path('/tmp/red-compose-float.png')
153 assert img.compare_to(img3) == 0
156def test_compose_size_difference():
157 img = image.from_data_url(red)
158 img2 = image.from_path('/tmp/x.png')
159 img = img.compose(img2)
160 img3 = image.from_path('/tmp/red-compose-size-diff.png')
161 assert img.compare_to(img3) == 0
164def test_compose():
165 img = image.from_data_url(red)
166 img2 = image.from_path('/tmp/blue.png')
167 img3 = image.from_path('/tmp/red-compose.png')
168 img = img.compose(img2)
169 assert img.compare_to(img3) == 0
172def test_to_bytes():
173 img = image.from_size((2.0, 2.0))
174 img.to_path('/tmp/img.png')
175 file = open('/tmp/img.png', 'rb')
176 byt = file.read()
177 assert img.to_bytes('image/png') == byt
180def test_to_path():
181 img = image.from_size((10.0, 10.0))
182 assert '/tmp/img.png' == img.to_path('/tmp/img.png')
183 assert os.path.isfile('/tmp/img.png')
186def test_to_array():
187 img = image.from_size((2.0, 2.0), color=(1, 2, 3, 4))
188 assert img.to_array().tolist() == ([[[1, 2, 3, 4], [1, 2, 3, 4]], [[1, 2, 3, 4], [1, 2, 3, 4]]])
191def test_add_text():
192 img = image.from_data_url(red)
193 img.add_text('FOOBAR', 10, 10, (0, 255, 0, 255))
194 img2 = image.from_path('/tmp/red-text.png')
195 assert img.compare_to(img2) == 0
198# box is not drawn around all edges
199def test_add_box():
200 img = image.from_data_url(red)
201 img = img.add_box((0, 255, 0, 255))
202 img2 = image.from_path('/tmp/red-box.png')
203 assert img.compare_to(img2) == 0