Giant blob of minor changes
[dotfiles/.git] / .config / GIMP / 2.10 / plug-ins / plugin-resynth-enlarge.py
1 #!/usr/bin/env python
2
3 '''
4 Gimp plugin "Enlarge and resynthesize"
5
6 Author:
7 lloyd konneker
8 Based on smart_enlarge.scm 2000 by Paul Harrison.
9
10 Version:
11 1.0 lloyd konneker lkk 2010 Initial version in python.
12
13 License:
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 GNU General Public License for more details.
24
25 The GNU Public License is available at
26 http://www.gnu.org/copyleft/gpl.html
27 '''
28
29 from gimpfu import *
30
31 gettext.install("resynthesizer", gimp.locale_directory, unicode=True)
32
33 def plugin_main(image, drawable, scale_factor):
34   '''
35   Algorithm:
36   
37   Scale image up.
38   Resynthesize with:
39     corpus = original size image
40     in map = original size image but scaled up and down to blur
41     out map = scaled up image
42     
43   This restores the detail that scaling up looses.
44   It maintains the aspect ratio of all image features.
45   
46   Unlike the original smart-enlarge.scm, this alters the original image.
47   
48   original did not accept an alpha channel
49   '''
50
51   
52   temp_image1 = pdb.gimp_image_duplicate(image)  # duplicate for in map
53   if not temp_image1:
54     raise RuntimeError, "Failed duplicate image"
55   temp_image2 = pdb.gimp_image_duplicate(image)  # duplicate for corpus
56   if not temp_image2:
57     raise RuntimeError, "Failed duplicate image"
58   temp_layer1 = pdb.gimp_image_get_active_layer(temp_image1)
59   if not temp_layer1:
60     raise RuntimeError, "Failed get active layer"
61   temp_layer2 = pdb.gimp_image_get_active_layer(temp_image2)
62   if not temp_layer2:
63     raise RuntimeError, "Failed get active layer"
64
65   # scale input map down and back, to blur
66   width = pdb.gimp_drawable_width(drawable)
67   height = pdb.gimp_drawable_height(drawable)
68   pdb.gimp_image_scale(temp_image1, width/scale_factor, height/scale_factor)
69   pdb.gimp_image_scale(temp_image1, width, height)
70     
71   # scale up the image
72   pdb.gimp_image_scale(image, width * scale_factor, height*scale_factor)
73
74   # Resynthesize to restore details.
75   # Note there should not be a selection. TODO
76   pdb.plug_in_resynthesizer(
77       image,
78       drawable,
79       0, 0, 0,
80       temp_layer2.ID,  # corpus
81       temp_layer1.ID,  # input map
82       drawable.ID,     # output map is scaled up original itself
83       1.0, 0.117, 8, 500) 
84
85   pdb.gimp_image_delete(temp_image1)
86   pdb.gimp_image_delete(temp_image2)
87
88
89 if __name__ == "__main__" :
90   register(
91     "python_fu_enlarge_resynthesized",
92     N_("Enlarge image and synthesize to sharpen."),
93     "Requires separate resynthesizer plugin",
94     "Lloyd Konneker",
95     "Copyright 2000 Paul Harrison, 2010 Lloyd Konneker",
96     "2010",
97     N_("Enlarge & sharpen..."),
98     "RGB*, GRAY*",
99     [
100       (PF_IMAGE, "image",       "Input image", None),
101       (PF_DRAWABLE, "drawable", "Input drawable", None),
102       (PF_ADJUSTMENT, "scale_factor", _("Scale by:"), 2, (1, 32, 0.1))
103     ],
104     [],
105     plugin_main,
106     menu="<Image>/Filters/Enhance",
107     domain=("resynthesizer", gimp.locale_directory)
108     )
109
110   main()
111   
112