Debian Codegraph @

test/test_get_info.py

raw
1 import os
2 import unittest
3
4 import imagesize
5
6
7 imagedir = os.path.join(os.path.dirname(__file__), "images")
8 ROTATED_HEIC = os.path.join(imagedir, "test_rotated.heic")
9 if not os.path.exists(ROTATED_HEIC):
10 ROTATED_HEIC = os.path.join(imagedir, "test-rotated.heic")
11 ROTATED_AVIF = os.path.join(imagedir, "test-rotated.avif")
12
13
14 class GetInfoTest(unittest.TestCase):
15 def test_get_info_defaults(self):
16 info = imagesize.get_info(os.path.join(imagedir, "test.png"))
17 self.assertEqual(info.width, 802)
18 self.assertEqual(info.height, 670)
19 self.assertEqual(info.xdpi, 72)
20 self.assertEqual(info.ydpi, 72)
21 self.assertEqual(info.colors, 16777216)
22 self.assertEqual(info.channels, 4)
23
24 def test_get_info_selective_fields(self):
25 info = imagesize.get_info(os.path.join(imagedir, "test.png"), size=True, dpi=False, colors=False, channels=False, exif_rotation=False)
26 self.assertEqual(info.width, 802)
27 self.assertEqual(info.height, 670)
28 self.assertEqual(info.xdpi, -1)
29 self.assertEqual(info.ydpi, -1)
30 self.assertEqual(info.colors, -1)
31 self.assertEqual(info.channels, -1)
32 self.assertEqual(info.rotation, -1)
33
34 def test_legacy_aliases(self):
35 size = imagesize.get(os.path.join(imagedir, "test.jpg"))
36 dpi = imagesize.getDPI(os.path.join(imagedir, "test.jpg"))
37 self.assertEqual(size, (802, 670))
38 self.assertEqual(dpi, (72, 72))
39
40 def test_get_info_invalid_input(self):
41 with self.assertRaises(FileNotFoundError):
42 imagesize.get_info(os.path.join(imagedir, "missing-file.png"))
43
44 def test_get_info_channels_jpeg(self):
45 info = imagesize.get_info(os.path.join(imagedir, "test.jpg"))
46 self.assertEqual(info.channels, 3)
47
48 def test_shortcuts_invalid_input(self):
49 missing = os.path.join(imagedir, "missing-file.png")
50 self.assertEqual(imagesize.get(missing), (-1, -1))
51 self.assertEqual(imagesize.getDPI(missing), (-1, -1))
52
53 def test_get_info_applies_exif_rotation_by_default(self):
54 info = imagesize.get_info(os.path.join(imagedir, "test-rotated.jpg"), dpi=False, colors=False)
55 self.assertEqual(info.width, 670)
56 self.assertEqual(info.height, 802)
57 self.assertEqual(info.rotation, 6)
58
59
60 def test_get_info_can_disable_exif_rotation(self):
61 info = imagesize.get_info(os.path.join(imagedir, "test-rotated.jpg"), dpi=False, colors=False, exif_rotation=False)
62 self.assertEqual(info.width, 802)
63 self.assertEqual(info.height, 670)
64 self.assertEqual(info.rotation, 6)
65
66
67 def test_get_info_applies_tiff_rotation_by_default(self):
68 info = imagesize.get_info(os.path.join(imagedir, "test-rotated.tiff"), dpi=False, colors=False)
69 self.assertEqual(info.width, 670)
70 self.assertEqual(info.height, 802)
71 self.assertEqual(info.rotation, 6)
72
73
74 def test_get_info_can_disable_tiff_rotation(self):
75 info = imagesize.get_info(os.path.join(imagedir, "test-rotated.tiff"), dpi=False, colors=False, exif_rotation=False)
76 self.assertEqual(info.width, 802)
77 self.assertEqual(info.height, 670)
78 self.assertEqual(info.rotation, 6)
79
80 def test_get_info_applies_heic_rotation_by_default(self):
81 info = imagesize.get_info(ROTATED_HEIC, dpi=False, colors=False)
82 raw = imagesize.get_info(ROTATED_HEIC, dpi=False, colors=False, exif_rotation=False)
83 expected = (raw.height, raw.width) if raw.rotation in {5, 6, 7, 8} else (raw.width, raw.height)
84 self.assertEqual((info.width, info.height), expected)
85 self.assertEqual(info.rotation, raw.rotation)
86
87 def test_get_info_can_disable_heic_rotation(self):
88 info = imagesize.get_info(ROTATED_HEIC, dpi=False, colors=False, exif_rotation=False)
89 self.assertEqual(info.width, 1440)
90 self.assertEqual(info.height, 960)
91
92 def test_get_info_applies_avif_rotation_by_default(self):
93 info = imagesize.get_info(ROTATED_AVIF, dpi=False, colors=False)
94 self.assertEqual(info.width, 420)
95 self.assertEqual(info.height, 630)
96 self.assertEqual(info.rotation, 6)
97
98 def test_get_info_can_disable_avif_rotation(self):
99 info = imagesize.get_info(ROTATED_AVIF, dpi=False, colors=False, exif_rotation=False)
100 self.assertEqual(info.width, 630)
101 self.assertEqual(info.height, 420)
102 self.assertEqual(info.rotation, 6)