Add parameter spacing_mm to function tile().
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 5 Jun 2019 04:26:26 +0000 (06:26 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 5 Jun 2019 04:26:26 +0000 (06:26 +0200)
gimp_passport.py
test_gimp_passport.py

index 8b82675..03a3318 100644 (file)
@@ -42,24 +42,37 @@ def make_passport(img_rgba: Image, fpr: float, width_mm: float, height_mm: float
     return pic
 
 
-def tile(img: Image, paper_width_mm: float, paper_height_mm: float, margin_mm: float) -> Image:
+def tile(img: Image, paper_width_mm: float, paper_height_mm: float, margin_mm: float,
+         spacing_mm: float) -> Image:
+    """Create and return image representing paper with specified dimensions and copy the given
+    image as often as possible to the paper.
+
+    :param img: source image that should be copied.
+    :param paper_width_mm: width of the image representing the paper
+    :param paper_height_mm: height of the image representing the paper
+    :param margin_mm: Free space from the paper borders to the edges of the tiled images.
+    :param spacing_mm: Additional space between tiled images.
+    """
     dpi = img.info['dpi'][0]
     paper_width_pixel = mm_to_pixel(paper_width_mm, dpi)
     paper_height_pixel = mm_to_pixel(paper_height_mm, dpi)
     margin_pixel = mm_to_pixel(margin_mm, dpi)
+    spacing_pixel = mm_to_pixel(spacing_mm, dpi)
     paper = Image.new('RGB', (paper_width_pixel, paper_height_pixel), 'white')
     paper.info['dpi'] = (dpi, dpi)
+    top = margin_pixel
     for iy in count():
-        top = margin_pixel + iy * img.height
         bottom = top + img.height
         if bottom > paper_height_pixel - margin_pixel:
             break
+        left = margin_pixel
         for ix in count():
-            left = margin_pixel + ix * img.width
             right = left + img.width
             if right > paper_width_pixel - margin_pixel:
                 break
             paper.paste(img, (left, top))
+            left = right + spacing_pixel
+        top = bottom + spacing_pixel
     return paper
 
 
@@ -67,7 +80,7 @@ def main(paper_width_mm: float, paper_height_mm: float, paper_margin_mm: float,
          photo_width_mm: float, photo_height_mm: float, source_image: str, dest_image: str):
     img = Image.open(source_image)
     photo = make_passport(img, 0.75, photo_width_mm, photo_height_mm)
-    paper = tile(photo, paper_width_mm, paper_height_mm, paper_margin_mm)
+    paper = tile(photo, paper_width_mm, paper_height_mm, paper_margin_mm, 0.)
     paper.save(dest_image, dpi=paper.info['dpi'])
 
 
index 2893e44..946a94f 100644 (file)
@@ -26,7 +26,7 @@ class TestMakePassport(unittest.TestCase):
         img.info['dpi'] = (dpi, dpi)
         draw = ImageDraw.ImageDraw(img)
         draw.rectangle((0, 0, img.width, img.height-1), outline='black')
-        paper = tile(img, 150, 100, 4)
+        paper = tile(img, 150, 100, 4, 0.)
         self.assertIsInstance(paper, Image.Image)
 
     def test_main(self):