Expose parameter fpr (face to picture ratio).
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 1 Jul 2019 20:46:32 +0000 (22:46 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 1 Jul 2019 20:46:32 +0000 (22:46 +0200)
gimp_passport.py
gimp_passport_plugin.py
test_gimp_passport.py

index 4350108..4da72c7 100644 (file)
@@ -161,10 +161,14 @@ def downsample_large(img: Image, max_dpi: Optional[int]) -> Image:
 
 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,
+         fpr: float,
          max_dpi: Optional[int],
          bbox: Tuple[int,int,int,int],
          source_image: Image) -> Image:
-    passport_tile = make_passport(source_image, bbox, 0.75, photo_width_mm, photo_height_mm)
+    """
+    :param fpr: face to picture ratio, e.g. 2/3
+    """
+    passport_tile = make_passport(source_image, bbox, fpr, 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)
@@ -180,13 +184,17 @@ def process(paper_width_mm: float, paper_height_mm: float, paper_margin_mm: floa
 
 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,
+         fpr: float,
          max_dpi: Optional[int],
          bbox: Tuple[int,int,int,int],
          source_image: str, dest_image: str):
+    """
+    :param fpr: face to picture ratio, e.g. 2/3
+    """
     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)
+            fpr, max_dpi, bbox, orig_photo)
     paper.save(dest_image, dpi=paper.info['dpi'])
 
 
@@ -199,6 +207,7 @@ if __name__ == '__main__':
     parser.add_argument('--photo-width', type=float, metavar='mm', default=35., help='passport photo width in mm (default: 35)')
     parser.add_argument('--photo-height', type=float, metavar='mm', default=45., help='passport photo height in mm (default: 45)')
     parser.add_argument('--photo-spacing', type=float, metavar='mm', default=0., help='space between passport photos in mm (default: 0)')
+    parser.add_argument('--fpr', type=float, default=0.75, help='face to picture ratio')
     parser.add_argument('--max-dpi', type=int, metavar='dpi', help='if given, scale the final image down to dpi if resultion is larger')
     parser.add_argument('left', type=int, help='number of pixels from left image border to left face border (inclusive)')
     parser.add_argument('top', type=int, help='number of pixels from top image border to top face border (inclusive)')
@@ -208,7 +217,7 @@ if __name__ == '__main__':
     parser.add_argument('dest', help='destination image')
     args = parser.parse_args()
     main(args.paper_width, args.paper_height, args.paper_margin, args.photo_width, args.photo_height,
-         args.photo_spacing, args.max_dpi,
+         args.photo_spacing, args.fpr, args.max_dpi,
          (args.left, args.top, args.right, args.bottom),
          args.source, args.dest)
 
index 4804140..1ed2038 100755 (executable)
@@ -5,7 +5,7 @@ import PIL.Image
 import gimp_passport
 
 
-def create_passport(image, drawable, paper_height_mm, paper_width_mm):
+def create_passport(image, drawable, paper_height_mm, paper_width_mm, fpr):
     # Note: for interactive sessions you can get the image via:
     # image = gimp.image_list()[0]
     # drawable = pdb.gimp_image_get_active_drawable(image)
@@ -31,7 +31,7 @@ def create_passport(image, drawable, paper_height_mm, paper_width_mm):
 
     # perform processing
     pil_paper = gimp_passport.process(paper_width_mm, paper_height_mm, margin_mm, photo_width_mm, photo_height_mm, spacing_mm, 
-                                      None, (left, top, right, bottom), pil_img)
+                                      fpr, None, (left, top, right, bottom), pil_img)
 
     # copy processed PIL to new GIMP image
     image_new = pdb.gimp_image_new(pil_paper.width, pil_paper.height, pdb.gimp_image_base_type(image))
@@ -55,6 +55,7 @@ register(
         [
             (PF_INT, "paper_height_mm", "Paper height in mm", 100),
             (PF_INT, "paper_width_mm", "Paper width in mm", 150),
+            (PF_FLOAT, "fpr", "Face to picture ratio", 0.75),
         ],
         [
             (PF_IMAGE, "image", "passport image for printout"),
index 72362b9..bf6ab96 100644 (file)
@@ -85,7 +85,7 @@ class TestMakePassport(unittest.TestCase):
 
     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)
+        img = process(150., 100., 4., 35., 45., 1., 0.75, None, (1108, 280, 2180, 1620), source)
         dpi = img.info['dpi']
         self.assertEqual(*dpi)
         dpi = dpi[0]
@@ -97,7 +97,7 @@ class TestMakePassport(unittest.TestCase):
         dest = 'test_image_result.jpg'
         if os.path.exists(dest):
             os.remove(dest)
-        main(150., 100., 4., 35., 45., 1., None, (1108, 280, 2180, 1620), source, dest)
+        main(150., 100., 4., 35., 45., 1., 0.75, None, (1108, 280, 2180, 1620), source, dest)
         self.assertTrue(os.path.exists(dest))
         img = Image.open(dest)
         dpi = img.info['dpi']