Debian Codegraph @

test/test_get.py

raw
1 import os
2 import unittest
3 from pathlib import Path
4
5 import imagesize
6
7 imagedir = os.path.join(os.path.dirname(__file__), "images")
8 imagedir_bytes = imagedir.encode("utf-8")
9 bmpfile = os.path.join(imagedir, "test_24.bmp")
10 bmpfile_bytes = os.path.join(imagedir_bytes, b"test_24.bmp")
11 ROTATED_JPEG = os.path.join(imagedir, "test-rotated.jpg")
12 ROTATED_TIFF = os.path.join(imagedir, "test-rotated.tiff")
13 ROTATED_AVIF = os.path.join(imagedir, "test-rotated.avif")
14 ROTATED_HEIC = os.path.join(imagedir, "test_rotated.heic")
15 if not os.path.exists(ROTATED_HEIC):
16 ROTATED_HEIC = os.path.join(imagedir, "test-rotated.heic")
17
18
19 class GetTest(unittest.TestCase):
20 def test_load_png(self):
21 width, height = imagesize.get(os.path.join(imagedir, "test.png"))
22 self.assertEqual(width, 802)
23 self.assertEqual(height, 670)
24
25 def test_load_jpeg(self):
26 width, height = imagesize.get(os.path.join(imagedir, "test.jpg"))
27 self.assertEqual(width, 802)
28 self.assertEqual(height, 670)
29
30 def test_load_jpeg2000(self):
31 width, height = imagesize.get(os.path.join(imagedir, "test.jp2"))
32 self.assertEqual(width, 802)
33 self.assertEqual(height, 670)
34
35 def test_load_jpeg2000_with_boxes_before_header(self):
36 path = os.path.join(imagedir, "issue84.jp2")
37 width, height = imagesize.get(path)
38 self.assertEqual(width, 1200)
39 self.assertEqual(height, 778)
40
41 info = imagesize.get_info(path)
42 self.assertEqual(info.width, 1200)
43 self.assertEqual(info.height, 778)
44
45 def test_load_gif(self):
46 width, height = imagesize.get(os.path.join(imagedir, "test.gif"))
47 self.assertEqual(width, 802)
48 self.assertEqual(height, 670)
49
50 def test_load_bmp(self):
51 width, height = imagesize.get(bmpfile)
52 self.assertEqual(width, 100)
53 self.assertEqual(height, 300)
54
55 def test_bigendian_tiff(self):
56 width, height = imagesize.get(os.path.join(imagedir, "test.tiff"))
57 self.assertEqual(width, 802)
58 self.assertEqual(height, 670)
59
60 def test_load_svg(self):
61 width, height = imagesize.get(os.path.join(imagedir, "test.svg"))
62 self.assertEqual(width, 90)
63 self.assertEqual(height, 60)
64
65 def test_load_svg_float_length(self):
66 from io import BytesIO
67
68 filelike = BytesIO(b'<svg width="25.4mm" height="12.7mm" xmlns="http://www.w3.org/2000/svg"></svg>')
69 width, height = imagesize.get(filelike)
70 self.assertEqual(width, 96)
71 self.assertEqual(height, 48)
72
73 def test_littleendian_tiff(self):
74 width, height = imagesize.get(os.path.join(imagedir, "multipage_tiff_example.tif"))
75 self.assertEqual(width, 800)
76 self.assertEqual(height, 600)
77
78 def test_load_svg_pt(self):
79 from io import BytesIO
80
81 svg = b'<svg xmlns="http://www.w3.org/2000/svg" width="72pt" height="36pt"></svg>'
82 width, height = imagesize.get(BytesIO(svg))
83 self.assertEqual(width, 96)
84 self.assertEqual(height, 48)
85
86 def test_load_svg_single_quoted_size(self):
87 from io import BytesIO
88
89 svg = b"<svg xmlns=\"http://www.w3.org/2000/svg\" width='90' height='60'></svg>"
90 width, height = imagesize.get(BytesIO(svg))
91 self.assertEqual(width, 90)
92 self.assertEqual(height, 60)
93
94 def test_load_svg_single_quoted_pt(self):
95 from io import BytesIO
96
97 svg = b"<svg xmlns=\"http://www.w3.org/2000/svg\" width='72pt' height='36pt'></svg>"
98 width, height = imagesize.get(BytesIO(svg))
99 self.assertEqual(width, 96)
100 self.assertEqual(height, 48)
101
102 def test_load_svg_ignores_stroke_width(self):
103 from io import BytesIO
104
105 svg = (
106 b"<svg xmlns=\"http://www.w3.org/2000/svg\" "
107 b"width='90' height='60' stroke-width=\"2\"></svg>"
108 )
109 width, height = imagesize.get(BytesIO(svg))
110 self.assertEqual(width, 90)
111 self.assertEqual(height, 60)
112
113 def test_load_svg_mismatched_quotes(self):
114 from io import BytesIO
115
116 svg = b"<svg xmlns='http://www.w3.org/2000/svg' width=\"90' height='60'></svg>"
117 width, height = imagesize.get(BytesIO(svg))
118 self.assertEqual(width, -1)
119 self.assertEqual(height, -1)
120
121 def test_load_png_filelike(self):
122 """ test_load_png_filelike
123 """
124 from io import BytesIO
125
126 _ = os.path.join(imagedir, "test.png")
127 with open(_, "rb") as fhandle:
128 filelike = BytesIO(fhandle.read())
129
130 width, height = imagesize.get(filelike)
131 self.assertEqual(width, 802)
132 self.assertEqual(height, 670)
133
134 def test_netpbm(self):
135 for test_type in ["test", "test-ascii"]:
136 for test_ext in ["pbm", "pgm", "ppm"]:
137 test_file = os.path.join(imagedir, "{}.{}".format(test_type, test_ext))
138 width, height = imagesize.get(test_file)
139 self.assertEqual(width, 65)
140 self.assertEqual(height, 20)
141
142 def test_load_png_bytes(self):
143 width, height = imagesize.get(os.path.join(imagedir_bytes, b"test.png"))
144 self.assertEqual(width, 802)
145 self.assertEqual(height, 670)
146
147 def test_load_jpeg_bytes(self):
148 width, height = imagesize.get(os.path.join(imagedir_bytes, b"test.jpg"))
149 self.assertEqual(width, 802)
150 self.assertEqual(height, 670)
151
152 def test_load_jpeg2000_bytes(self):
153 width, height = imagesize.get(os.path.join(imagedir_bytes, b"test.jp2"))
154 self.assertEqual(width, 802)
155 self.assertEqual(height, 670)
156
157 def test_load_gif_bytes(self):
158 width, height = imagesize.get(os.path.join(imagedir_bytes, b"test.gif"))
159 self.assertEqual(width, 802)
160 self.assertEqual(height, 670)
161
162 def test_load_bmp_bytes(self):
163 width, height = imagesize.get(bmpfile_bytes)
164 self.assertEqual(width, 100)
165 self.assertEqual(height, 300)
166
167 def test_bigendian_tiff_bytes(self):
168 width, height = imagesize.get(os.path.join(imagedir_bytes, b"test.tiff"))
169 self.assertEqual(width, 802)
170 self.assertEqual(height, 670)
171
172 def test_load_svg_bytes(self):
173 width, height = imagesize.get(os.path.join(imagedir_bytes, b"test.svg"))
174 self.assertEqual(width, 90)
175 self.assertEqual(height, 60)
176
177 def test_littleendian_tiff_bytes(self):
178 width, height = imagesize.get(os.path.join(imagedir_bytes, b"multipage_tiff_example.tif"))
179 self.assertEqual(width, 800)
180 self.assertEqual(height, 600)
181
182 def test_load_avif(self):
183 width, height = imagesize.get(os.path.join(imagedir, "test.avif"))
184 self.assertEqual(width, 630)
185 self.assertEqual(height, 420)
186
187 def test_load_avif_bytes(self):
188 width, height = imagesize.get(os.path.join(imagedir_bytes, b"test.avif"))
189 self.assertEqual(width, 630)
190 self.assertEqual(height, 420)
191
192 def test_load_heic(self):
193 width, height = imagesize.get(os.path.join(imagedir, "test.heic"))
194 self.assertEqual(width, 1440)
195 self.assertEqual(height, 960)
196
197 def test_load_heic_bytes(self):
198 width, height = imagesize.get(os.path.join(imagedir_bytes, b"test.heic"))
199 self.assertEqual(width, 1440)
200 self.assertEqual(height, 960)
201
202 def test_load_webp_vp8x(self):
203 """Test VP8X format WebP file parsing.
204
205 VP8X format stores dimensions as (width-1, height-1) in the header,
206 so the parser must add 1 to get the actual dimensions.
207 """
208 width, height = imagesize.get(os.path.join(imagedir, "test_vp8x.webp"))
209 self.assertEqual(width, 200)
210 self.assertEqual(height, 1)
211
212 def test_load_png_path(self):
213 width, height = imagesize.get(Path(imagedir, "test.png"))
214 self.assertEqual(width, 802)
215 self.assertEqual(height, 670)
216
217 def test_load_jpeg_path(self):
218 width, height = imagesize.get(Path(imagedir, "test.jpg"))
219 self.assertEqual(width, 802)
220 self.assertEqual(height, 670)
221
222 def test_load_jpeg2000_path(self):
223 width, height = imagesize.get(Path(imagedir, "test.jp2"))
224 self.assertEqual(width, 802)
225 self.assertEqual(height, 670)
226
227 def test_load_gif_path(self):
228 width, height = imagesize.get(Path(imagedir, "test.gif"))
229 self.assertEqual(width, 802)
230 self.assertEqual(height, 670)
231
232 def test_load_bmp_path(self):
233 width, height = imagesize.get(Path(bmpfile))
234 self.assertEqual(width, 100)
235 self.assertEqual(height, 300)
236
237 def test_bigendian_tiff_path(self):
238 width, height = imagesize.get(Path(imagedir, "test.tiff"))
239 self.assertEqual(width, 802)
240 self.assertEqual(height, 670)
241
242 def test_load_svg_path(self):
243 width, height = imagesize.get(Path(imagedir, "test.svg"))
244 self.assertEqual(width, 90)
245 self.assertEqual(height, 60)
246
247 def test_littleendian_tiff_path(self):
248 width, height = imagesize.get(Path(imagedir, "multipage_tiff_example.tif"))
249 self.assertEqual(width, 800)
250 self.assertEqual(height, 600)
251
252 def test_load_jpeg_with_exif_rotation_default(self):
253 width, height = imagesize.get(ROTATED_JPEG)
254 self.assertEqual(width, 670)
255 self.assertEqual(height, 802)
256
257 def test_load_jpeg_with_exif_rotation_disabled(self):
258 width, height = imagesize.get(ROTATED_JPEG, exif_rotation=False)
259 self.assertEqual(width, 802)
260 self.assertEqual(height, 670)
261
262
263 def test_load_tiff_with_exif_rotation_default(self):
264 width, height = imagesize.get(ROTATED_TIFF)
265 self.assertEqual(width, 670)
266 self.assertEqual(height, 802)
267
268 def test_load_tiff_with_exif_rotation_disabled(self):
269 width, height = imagesize.get(ROTATED_TIFF, exif_rotation=False)
270 self.assertEqual(width, 802)
271 self.assertEqual(height, 670)
272
273 def test_load_heic_with_exif_rotation_default(self):
274 width, height = imagesize.get(ROTATED_HEIC)
275 info = imagesize.get_info(ROTATED_HEIC, dpi=False, colors=False, channels=False, exif_rotation=False)
276 expected = (info.height, info.width) if info.rotation in {5, 6, 7, 8} else (info.width, info.height)
277 self.assertEqual((width, height), expected)
278
279 def test_load_heic_with_exif_rotation_disabled(self):
280 width, height = imagesize.get(ROTATED_HEIC, exif_rotation=False)
281 self.assertEqual(width, 1440)
282 self.assertEqual(height, 960)
283
284 def test_load_avif_with_exif_rotation_default(self):
285 width, height = imagesize.get(ROTATED_AVIF)
286 self.assertEqual(width, 420)
287 self.assertEqual(height, 630)
288
289 def test_load_avif_with_exif_rotation_disabled(self):
290 width, height = imagesize.get(ROTATED_AVIF, exif_rotation=False)
291 self.assertEqual(width, 630)
292 self.assertEqual(height, 420)