5564518bc51b067da16ef76c9ef15f0a5455199e
[toast/gimp_passport.git] / gimp_passport_plugin.py
1 #!/usr/bin/python
2
3 from gimpfu import *
4 import PIL.Image
5 import gimp_passport
6
7
8 def create_passport(image, drawable, paper_height_mm, paper_width_mm):
9     # Note: for interactive sessions you can get the image via:
10     # image = gimp.image_list()[0]
11     # drawable = pdb.gimp_image_get_active_drawable(image)
12
13     # get face extent
14     non_empty, left, top, right, bottom = pdb.gimp_selection_bounds(image)
15     if not non_empty:
16         pdb.gimp_message('Please select the face first.')
17         return image
18
19     # pdb.gimp_image_find_next_guide(image, 0)
20     # pdb.gimp_image_get_guide_orientation(image, 6)
21     # pdb.gimp_image_get_guide_position(image, 6)
22
23     photo_width_mm = 35.
24     photo_height_mm = 45.
25     fpr = 0.75
26     margin_mm = 4.
27     spacing_mm = 1.
28
29     # convert GIMP image to PIL image:
30     rgn = drawable.get_pixel_rgn(0, 0, drawable.width, drawable.height)
31     pil_img = PIL.Image.frombytes('RGB', (drawable.width, drawable.height), rgn[:, :])
32
33     # perform processing
34     # pil_paper = pil_img.crop((left, top, right, bottom))
35     pil_photo = gimp_passport.make_passport(pil_img, (left, top, right, bottom), fpr, photo_width_mm, photo_height_mm)
36     pil_paper = gimp_passport.tile(pil_photo, paper_width_mm, paper_height_mm, margin_mm, spacing_mm)
37
38     # copy processed PIL to new GIMP image
39     image_new = pdb.gimp_image_new(pil_paper.width, pil_paper.height, pdb.gimp_image_base_type(image))
40     layer = gimp.Layer(image_new, "passport", pil_paper.width, pil_paper.height, pdb.gimp_image_base_type(image), 100, NORMAL_MODE)
41     image_new.add_layer(layer)
42     rgn = layer.get_pixel_rgn(0, 0, layer.width, layer.height, True)
43     rgn[:, :] = pil_paper.tobytes()
44     gimp.Display(image_new)
45     return image_new
46
47
48 register(
49         "create_passport",
50         "Creates a printable passport image (contact sheet) from a photo",
51         "Select the face and call the plugin",
52         "Toastfreeware",
53         "Toastfreeware",
54         "2019",
55         "<Image>/Filters/Map/Create _Passport...",
56         "RGB*, GRAY*",
57         [
58             (PF_INT, "paper_height_mm", "Paper height in mm", 100),
59             (PF_INT, "paper_width_mm", "Paper width in mm", 150),
60         ],
61         [
62             (PF_IMAGE, "image", "passport image for printout"),
63         ],
64         create_passport)
65
66
67 main()
68