Create function process() that does the main work of main().
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 1 Jul 2019 18:42:20 +0000 (20:42 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 1 Jul 2019 18:42:20 +0000 (20:42 +0200)
gimp_passport.py
test_gimp_passport.py

index 1aafe73..4350108 100644 (file)
@@ -159,13 +159,12 @@ def downsample_large(img: Image, max_dpi: Optional[int]) -> Image:
     return img
 
 
-def main(paper_width_mm: float, paper_height_mm: float, paper_margin_mm: float,
+def process(paper_width_mm: float, paper_height_mm: float, paper_margin_mm: float,
          photo_width_mm: float, photo_height_mm: float, photo_spacing_mm: float,
          max_dpi: Optional[int],
          bbox: Tuple[int,int,int,int],
-         source_image: str, dest_image: str):
-    orig_photo = Image.open(source_image)
-    passport_tile = make_passport(orig_photo, bbox, 0.75, photo_width_mm, photo_height_mm)
+         source_image: Image) -> Image:
+    passport_tile = make_passport(source_image, bbox, 0.75, photo_width_mm, photo_height_mm)
     dpi = passport_tile.info['dpi'][0]
     paper = Image.new('RGB', (mm_to_pixel(paper_width_mm, dpi), mm_to_pixel(paper_height_mm, dpi)), 'white')
     paper.info['dpi'] = (dpi, dpi)
@@ -176,7 +175,18 @@ def main(paper_width_mm: float, paper_height_mm: float, paper_margin_mm: float,
     cut_lines_xy(paper, columns, rows, passport_tile.size, margin, spacing)
     halo(paper, columns, rows, passport_tile.size, 1, margin, spacing)
     tile(paper, passport_tile, columns, rows, margin, spacing)
-    paper = downsample_large(paper, max_dpi)
+    return downsample_large(paper, max_dpi)
+
+
+def main(paper_width_mm: float, paper_height_mm: float, paper_margin_mm: float,
+         photo_width_mm: float, photo_height_mm: float, photo_spacing_mm: float,
+         max_dpi: Optional[int],
+         bbox: Tuple[int,int,int,int],
+         source_image: str, dest_image: str):
+    orig_photo = Image.open(source_image)
+    paper = process(paper_width_mm, paper_height_mm, paper_margin_mm,
+            photo_width_mm, photo_height_mm, photo_spacing_mm,
+            max_dpi, bbox, orig_photo)
     paper.save(dest_image, dpi=paper.info['dpi'])
 
 
index 31a05b8..72362b9 100644 (file)
@@ -2,7 +2,7 @@ import os
 import unittest
 from PIL import Image, ImageDraw
 from gimp_passport import mm_to_pixel, num_tiles, tile_edge, line, cut_lines, cut_lines_xy,  make_passport, halo, tile, \
-    downsample_large, main
+    downsample_large, process, main
 
 
 class TestMakePassport(unittest.TestCase):
@@ -83,6 +83,15 @@ class TestMakePassport(unittest.TestCase):
         self.assertEqual(int(round(source.height / 2)), dest.height)
         self.assertEqual((45, 45), dest.info['dpi'])
 
+    def test_process(self):
+        source = Image.new('RGB', (3000, 2000), 'white')
+        img = process(150., 100., 4., 35., 45., 1., None, (1108, 280, 2180, 1620), source)
+        dpi = img.info['dpi']
+        self.assertEqual(*dpi)
+        dpi = dpi[0]
+        self.assertEqual(mm_to_pixel(100., dpi), img.height)
+        self.assertEqual(mm_to_pixel(150., dpi), img.width)
+
     def test_main(self):
         source = 'test_image.jpg'
         dest = 'test_image_result.jpg'