Add DPI to output image.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 3 Jun 2019 21:28:26 +0000 (23:28 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Mon, 3 Jun 2019 21:50:01 +0000 (23:50 +0200)
gimp_passport.py

index af6e54c..63ae729 100644 (file)
@@ -2,6 +2,9 @@ import argparse
 from PIL import Image
 
 
 from PIL import Image
 
 
+MM_PER_INCH = 25.4
+
+
 def make_passport(img_rgba: Image, fpr: float, width_mm: float, height_mm: float) -> Image:
     """
     :param fpr: face to picture ratio, e.g. 2/3
 def make_passport(img_rgba: Image, fpr: float, width_mm: float, height_mm: float) -> Image:
     """
     :param fpr: face to picture ratio, e.g. 2/3
@@ -28,13 +31,15 @@ def make_passport(img_rgba: Image, fpr: float, width_mm: float, height_mm: float
     # cut image
     img = img_rgba.convert('RGB')  # drop alpha channel
     pic = img.crop(bbox_pic)
     # cut image
     img = img_rgba.convert('RGB')  # drop alpha channel
     pic = img.crop(bbox_pic)
+    dpi = int(round(pic_height / (height_mm / MM_PER_INCH)))
+    pic.info['dpi'] = (dpi, dpi)
     return pic
 
 
 def main(source_image: str, dest_image: str):
     img = Image.open(source_image)
     pic = make_passport(img, 0.75, 35., 45.)
     return pic
 
 
 def main(source_image: str, dest_image: str):
     img = Image.open(source_image)
     pic = make_passport(img, 0.75, 35., 45.)
-    pic.save(dest_image)
+    pic.save(dest_image, dpi=pic.info['dpi'])
 
 
 if __name__ == '__main__':
 
 
 if __name__ == '__main__':