Now using PIL to process GIMP image.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Thu, 20 Jun 2019 21:23:11 +0000 (23:23 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Thu, 20 Jun 2019 21:23:11 +0000 (23:23 +0200)
gimp_passport_plugin.py

index 12316f7..863d78c 100755 (executable)
@@ -1,19 +1,36 @@
 #!/usr/bin/python
 
 from gimpfu import *
-#import pdb
+import PIL.Image
+
 
 def create_passport(image, drawable, paper_height_mm, paper_width_mm):
     # Note: for interactive sessions you can get the image via:
     # image = gimp.image_list()[0]
+    # drawable = pdb.gimp_image_get_active_drawable(image)
+
+    # convert GIMP image to PIL image:
+    rgn = drawable.get_pixel_rgn(0, 0, drawable.width, drawable.height)
+    pil_img = PIL.Image.frombytes('RGB', (drawable.width, drawable.height), rgn[:, :])
 
+    # get face extent
     # non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(image)
 
     # pdb.gimp_image_find_next_guide(image, 0)
     # pdb.gimp_image_get_guide_orientation(image, 6)
     # pdb.gimp_image_get_guide_position(image, 6)
-    pdb.gimp_image_flip(image, 1)
-    return image
+
+    # perform processing
+    pil_img = pil_img.transpose(PIL.Image.ROTATE_180)
+
+    # copy processed PIL to new GIMP image
+    image_new = pdb.gimp_image_duplicate(image)
+    layer = gimp.Layer(image_new, "myname", image_new.width, image_new.height, image.active_layer.type, 100, NORMAL_MODE)
+    rgn = layer.get_pixel_rgn(0, 0, layer.width, layer.height, True)
+    rgn[:, :] = pil_img.tobytes()
+    image_new.add_layer(layer)
+    gimp.Display(image_new)
+    return image_new
 
 
 register(
@@ -26,10 +43,12 @@ register(
         "<Image>/Filters/Map/Create _Passport...",
         "RGB*, GRAY*",
         [
-                (PF_INT, "paper_height_mm", "Paper height in mm", 100),
-                (PF_INT, "paper_width_mm", "Paper width in mm", 150),
+            (PF_INT, "paper_height_mm", "Paper height in mm", 100),
+            (PF_INT, "paper_width_mm", "Paper width in mm", 150),
+        ],
+        [
+            (PF_IMAGE, "image", "passport image for printout"),
         ],
-        [],
         create_passport)