Downsample as first step, not last step.
[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, cut_lines_xy,  make_passport, halo, tile, \
5     downsample_large, process, 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_cut_lines_xy(self):
52         img = Image.new('RGB', (100, 60))
53         cut_lines_xy(img, 4, 3, (20, 9), 5, 2)
54
55     def test_halo(self):
56         img = Image.new('RGB', (100, 60))
57         halo(img, 4, 3, (20, 9), 1, 5, 2)
58
59     def test_tile(self):
60         dpi = 90
61         passport_tile = Image.new('RGB', (mm_to_pixel(35, dpi), mm_to_pixel(45, dpi)), 'lightgray')
62         passport_tile.info['dpi'] = (dpi, dpi)
63         draw = ImageDraw.ImageDraw(passport_tile)
64         draw.rectangle((0, 0, passport_tile.width-1, passport_tile.height-1), outline='black')
65
66         paper = Image.new('RGB', (mm_to_pixel(150, dpi), mm_to_pixel(100, dpi)), 'white')
67         paper.info['dpi'] = (dpi, dpi)
68         tile(paper, passport_tile, 4, 2, 5, 3)
69         self.assertIsInstance(paper, Image.Image)
70
71     def test_downsample_large(self):
72         dpi = 90
73         source = Image.new('RGB', (mm_to_pixel(35, dpi), mm_to_pixel(45, dpi)), 'lightgray')
74         source.info['dpi'] = (dpi, dpi)
75         dest = downsample_large(source, None)
76         self.assertEqual(source.size, dest.size)
77         dest = downsample_large(source, 2 * dpi)
78         self.assertEqual(source.size, dest.size)
79         dest = downsample_large(source, dpi)
80         self.assertEqual(source.size, dest.size)
81         dest = downsample_large(source, 45)
82         self.assertEqual(int(round(source.width / 2)), dest.width)
83         self.assertEqual(int(round(source.height / 2)), dest.height)
84         self.assertEqual((45, 45), dest.info['dpi'])
85
86     def test_process(self):
87         source = Image.new('RGB', (3000, 2000), 'white')
88         img = process(150., 100., 4., 35., 45., 1., 0.75, None, (1108, 280, 2180, 1620), source)
89         dpi = img.info['dpi']
90         self.assertEqual(*dpi)
91         dpi = dpi[0]
92         self.assertEqual(mm_to_pixel(100., dpi), img.height)
93         self.assertEqual(mm_to_pixel(150., dpi), img.width)
94
95     def test_main(self):
96         source = 'test_image.jpg'
97         dest = 'test_image_result.jpg'
98         if os.path.exists(dest):
99             os.remove(dest)
100         main(150., 100., 4., 35., 45., 1., 0.75, None, (1108, 280, 2180, 1620), source, dest)
101         self.assertTrue(os.path.exists(dest))
102         img = Image.open(dest)
103         dpi = img.info['dpi']
104         self.assertEqual(*dpi)
105         dpi = dpi[0]
106         self.assertEqual(mm_to_pixel(100., dpi), img.height)
107         self.assertEqual(mm_to_pixel(150., dpi), img.width)
108