Downsample as first step, not last step.
[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_width_mm, paper_height_mm, margin_mm, photo_width_mm,
9                     photo_height_mm, spacing_mm, fpr, max_dpi):
10     # Note: for interactive sessions you can get the image via:
11     # image = gimp.image_list()[0]
12     # drawable = pdb.gimp_image_get_active_drawable(image)
13
14     # get face extent
15     non_empty, left, top, right, bottom = pdb.gimp_selection_bounds(image)
16     if not non_empty:
17         pdb.gimp_message('Please select the face first.')
18         return image
19
20     # pdb.gimp_image_find_next_guide(image, 0)
21     # pdb.gimp_image_get_guide_orientation(image, 6)
22     # pdb.gimp_image_get_guide_position(image, 6)
23
24     # convert GIMP image to PIL image:
25     rgn = drawable.get_pixel_rgn(0, 0, drawable.width, drawable.height)
26     pil_img = PIL.Image.frombytes('RGB', (drawable.width, drawable.height), rgn[:, :])
27
28     # perform processing
29     pil_paper = gimp_passport.process(paper_width_mm, paper_height_mm, margin_mm, photo_width_mm, photo_height_mm, spacing_mm, 
30                                       fpr, max_dpi, (left, top, right, bottom), pil_img)
31
32     # copy processed PIL to new GIMP image
33     image_new = pdb.gimp_image_new(pil_paper.width, pil_paper.height, pdb.gimp_image_base_type(image))
34     layer = gimp.Layer(image_new, "passport", pil_paper.width, pil_paper.height, pdb.gimp_image_base_type(image), 100, NORMAL_MODE)
35     image_new.add_layer(layer)
36     rgn = layer.get_pixel_rgn(0, 0, layer.width, layer.height, True)
37     rgn[:, :] = pil_paper.tobytes()
38     pdb.gimp_image_set_resolution(image_new, *pil_paper.info['dpi'])
39     gimp.Display(image_new)
40     return image_new
41
42
43 register(
44         "create_passport",
45         "Creates a printable passport image (contact sheet) from a photo",
46         "Select the face and call the plugin",
47         "Toastfreeware",
48         "Toastfreeware",
49         "2019",
50         "<Image>/Filters/Map/Create _Passport...",
51         "RGB*, GRAY*",
52         [
53             (PF_FLOAT, "paper_width_mm", "Paper width in mm", 150.),
54             (PF_FLOAT, "paper_height_mm", "Paper height in mm", 100.),
55             (PF_FLOAT, "margin_mm", "Paper margin in mm", 4.),
56             (PF_FLOAT, "photo_width_mm", "Passport photo width in mm", 35.),
57             (PF_FLOAT, "photo_height_mm", "Passport photo height in mm", 45.),
58             (PF_FLOAT, "spacing_mm", "Space between passport photos in mm", 1.),
59             (PF_FLOAT, "fpr", "Face to picture ratio", 0.75),
60             (PF_INT, "max_dpi", "Maximum dots per inch", 600),
61         ],
62         [
63             (PF_IMAGE, "image", "passport image for printout"),
64         ],
65         create_passport)
66
67
68 main()
69