Update workflows/publish_pypi.yml
[manga-dl.git] / tests / images.py
blob8aff3580fd686dcca72757b48dc31fd12c5bc92d
1 import unittest
2 from os import path
4 from PIL import Image as PilImage
6 from manga_py import fs
7 from manga_py.manga_image import MangaImage
9 root_path = path.dirname(path.realpath(__file__))
11 files_paths = [
12 ['/files/img1.jpg', '/temp/img1.jpg'],
13 ['/files/img2.png', '/temp/img2.png'],
14 ['/files/img3.jpg', '/temp/img3.jpg'],
15 ['/files/img4.jpg', '/temp/img4.jpg'],
16 ['/files/img5.png', '/temp/img5.png'],
17 ['/files/img6.gif', '/temp/img6.gif'],
18 ['/files/img7.webp', '/temp/img7.webp'],
22 class TestImages(unittest.TestCase):
24 def test_manual_crop(self):
25 for file in files_paths:
26 fs.unlink(root_path + file[1])
28 image = PilImage.open(root_path + file[0])
29 sizes = image.size
30 image.close()
32 img = MangaImage(root_path + file[0])
33 img.crop_manual((10, 0, image.size[0], image.size[1]), root_path + file[1])
34 img.close()
36 cropped_image = PilImage.open(root_path + file[1])
37 cropped_sizes = cropped_image.size
38 cropped_image.close()
40 self.assertTrue((sizes[0] - cropped_sizes[0]) == 10)
42 def test_manual_crop_with_offsets(self):
43 for file in files_paths:
44 fs.unlink(root_path + file[1])
46 image = PilImage.open(root_path + file[0])
47 sizes = image.size
48 image.close()
50 img = MangaImage(root_path + file[0])
51 img.crop_manual_with_offsets((10, 0, 0, 0), root_path + file[1])
52 img.close()
54 cropped_image = PilImage.open(root_path + file[1])
55 cropped_sizes = cropped_image.size
56 cropped_image.close()
58 self.assertTrue((sizes[0] - cropped_sizes[0]) == 10)
60 def test_auto_crop1(self):
61 file = files_paths[0]
62 fs.unlink(root_path + file[1])
64 image = PilImage.open(root_path + file[0])
65 sizes = image.size
66 image.close()
68 img = MangaImage(root_path + file[0])
69 img.crop_auto(root_path + file[1])
70 img.close()
72 cropped_image = PilImage.open(root_path + file[1])
73 cropped_sizes = cropped_image.size
74 cropped_image.close()
76 self.assertTrue(sizes[0] > cropped_sizes[0])
78 def test_auto_crop2(self):
79 file = files_paths[1]
80 fs.unlink(root_path + file[1])
82 image = PilImage.open(root_path + file[0])
83 sizes = image.size
84 image.close()
86 img = MangaImage(root_path + file[0])
87 img.crop_auto(root_path + file[1])
88 img.close()
90 cropped_image = PilImage.open(root_path + file[1])
91 cropped_sizes = cropped_image.size
92 cropped_image.close()
94 self.assertTrue(sizes[0] == cropped_sizes[0])
96 def test_auto_crop3(self):
97 file = files_paths[4]
98 fs.unlink(root_path + file[1])
100 image = PilImage.open(root_path + file[0])
101 sizes = image.size
102 image.close()
104 img = MangaImage(root_path + file[0])
105 img.crop_auto(root_path + file[1])
106 img.close()
108 cropped_image = PilImage.open(root_path + file[1])
109 cropped_sizes = cropped_image.size
110 cropped_image.close()
112 self.assertTrue(sizes[0] == (2 + cropped_sizes[0])) # 2px black line
114 def test_image_not_found(self):
115 self.assertRaises(AttributeError, lambda: MangaImage(root_path))
117 def test_gray1(self):
118 file = files_paths[1]
119 fs.unlink(root_path + file[1])
121 image = MangaImage(root_path + file[0])
122 image.gray(root_path + file[1])
123 image.close()
125 image = PilImage.open(root_path + file[1])
126 index = image.mode.find('L')
127 image.close()
129 self.assertTrue(index == 0)
131 def test_convert(self):
132 file = files_paths[0][0]
133 image = MangaImage(root_path + file)
135 basename = file[0:file.find('.')]
136 basename = root_path + '/temp' + basename + '.bmp'
137 image.convert(basename)
138 image.close()
140 self.assertTrue(path.isfile(basename))