Add test case for function cut_lines().
[toast/gimp_passport.git] / test_gimp_passport.py
1 import os
2 import unittest
3 from PIL import Image, ImageDraw
4 from gimp_passport import mm_to_pixel, num_tiles, tile_edge, line, cut_lines, make_passport, tile, \
5     downsample_large, main
6
7
8 class TestMakePassport(unittest.TestCase):
9
10     def test_mm_to_pixel(self):
11         self.assertEqual(0, mm_to_pixel(0., 100))
12         self.assertEqual(200, mm_to_pixel(25.4, 200))
13         self.assertEqual(600, mm_to_pixel(2*25.4, 300))
14
15     def test_make_passport(self):
16         dpi = 90
17         img = Image.new('RGB', (mm_to_pixel(60, dpi), mm_to_pixel(80, dpi)), '#bbbbbb')
18         img.info['dpi'] = (dpi, dpi)
19         draw = ImageDraw.ImageDraw(img)
20         draw.rectangle((50, 60, 150, 170), fill='#00bbbbff')
21         pic = make_passport(img, (50, 60, 150, 170), 0.75, 35., 45.)
22         self.assertIsInstance(pic, Image.Image)
23
24     def test_num_tiles(self):
25         self.assertEqual(0, num_tiles(10., 11., 0., 0.))
26         self.assertEqual(1, num_tiles(10., 10., 0., 0.))
27         self.assertEqual(2, num_tiles(20., 10., 0., 0.))
28         self.assertEqual(1, num_tiles(20., 10., 1., 0.))
29         self.assertEqual(1, num_tiles(20., 10., 0., 1.))
30         self.assertEqual(3, num_tiles(30., 9., 0., 1.))
31         self.assertEqual(3, num_tiles(30., 9., 1., 0.))
32         self.assertEqual(2, num_tiles(30., 9., 1., 1.))
33
34     def test_tile_edge(self):
35         margin = 4
36         spacing = 3
37         length = 9
38         self.assertEqual(4, tile_edge(0, length, margin, spacing))
39         self.assertEqual(16, tile_edge(1, length, margin, spacing))
40         self.assertEqual(28, tile_edge(2, length, margin, spacing))
41
42     def test_line(self):
43         img = Image.new('RGB', (100, 60))
44         line(img, 30, 0)
45         line(img, 40, 1)
46
47     def test_cut_lines(self):
48         img = Image.new('RGB', (100, 60))
49         cut_lines(img, 4, 20, 5, 2, 0)
50
51     def test_tile(self):
52         dpi = 90
53         img = Image.new('RGB', (mm_to_pixel(35, dpi), mm_to_pixel(45, dpi)), 'lightgray')
54         img.info['dpi'] = (dpi, dpi)
55         draw = ImageDraw.ImageDraw(img)
56         draw.rectangle((0, 0, img.width, img.height-1), outline='black')
57         paper = tile(img, 150, 100, 4, 2.)
58         self.assertIsInstance(paper, Image.Image)
59
60     def test_downsample_large(self):
61         dpi = 90
62         source = Image.new('RGB', (mm_to_pixel(35, dpi), mm_to_pixel(45, dpi)), 'lightgray')
63         source.info['dpi'] = (dpi, dpi)
64         dest = downsample_large(source, None)
65         self.assertEqual(source.size, dest.size)
66         dest = downsample_large(source, 2 * dpi)
67         self.assertEqual(source.size, dest.size)
68         dest = downsample_large(source, dpi)
69         self.assertEqual(source.size, dest.size)
70         dest = downsample_large(source, 45)
71         self.assertEqual(int(round(source.width / 2)), dest.width)
72         self.assertEqual(int(round(source.height / 2)), dest.height)
73         self.assertEqual((45, 45), dest.info['dpi'])
74
75     def test_main(self):
76         source = 'test_image.jpg'
77         dest = 'test_image_result.jpg'
78         if os.path.exists(dest):
79             os.remove(dest)
80         main(150., 100., 4., 35., 45., 1., None, (1108, 280, 2180, 1620), source, dest)
81         self.assertTrue(os.path.exists(dest))
82         img = Image.open(dest)
83         dpi = img.info['dpi']
84         self.assertEqual(*dpi)
85         dpi = dpi[0]
86         self.assertEqual(mm_to_pixel(100., dpi), img.height)
87         self.assertEqual(mm_to_pixel(150., dpi), img.width)
88