Giant blob of minor changes
[dotfiles/.git] / .config / GIMP / 2.10 / plug-ins / plugin-uncrop.py
1 #!/usr/bin/env python
2
3 '''
4 Gimp plugin "Uncrop"
5
6 Increase image/canvas size and synthesize outer band from edge of original.
7
8 Author:
9 lloyd konneker, lkk
10
11 Version:
12 1.0 lkk 5/15/2009 Initial version in scheme, released to Gimp Registry.
13 1.1 lkk 9/21/2009 Translate to python.
14
15 License:
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 GNU General Public License for more details.
26
27 The GNU Public License is available at
28 http://www.gnu.org/copyleft/gpl.html
29
30
31 The effect for users:  
32 widens the field of view, maintaining perspective of original
33 Should be undoable, except for loss of selection.
34 Should work on any image type, any count of layers and channels (although only active layer is affected.)
35
36 Programming notes:
37 Scheme uses - in names, python uses _
38 Programming devt. cycle: 
39 Initial creation: cp foo.py ~/.gimp-2.6/scripts, chmod +x, start gimp
40 Refresh:  just copy, no need to restart gimp if the pdb registration is unchanged
41
42 IN: Nothing special.  The selection is immaterial but is not preserved.
43 OUT larger layer and image.  All other layers not enlarged.
44 '''
45
46 from gimpfu import *
47
48 gettext.install("resynthesizer", gimp.locale_directory, unicode=True)
49     
50 def resizeImageCentered(image, percentEnlarge):
51     # resize and center image by percent (converted to pixel units)
52     deltaFraction = (percentEnlarge / 100) + 1.0
53     priorWidth = pdb.gimp_image_width(image)
54     priorHeight = pdb.gimp_image_height(image)
55     deltaWidth = priorWidth * deltaFraction
56     deltaHeight = priorHeight * deltaFraction
57     centeredOffX = (deltaWidth - priorWidth)/  2 
58     centeredOffY = (deltaHeight - priorHeight) / 2 
59     pdb.gimp_image_resize(image, deltaWidth, deltaHeight, centeredOffX, centeredOffY)
60     #if not pdb.gimp_image_resize(image, deltaWidth, deltaHeight, centeredOffX, centeredOffY):
61     #    raise RuntimeError, "Failed resize"
62         
63 def shrinkSelectionByPercent(image, percent):
64     # shrink selection by percent (converted to pixel units)
65     deltaFraction = percent / 100
66     # convert to pixel dimensions
67     priorWidth = pdb.gimp_image_width(image)
68     priorHeight = pdb.gimp_image_height(image)
69     deltaWidth = priorWidth * deltaFraction
70     deltaHeight = priorHeight * deltaFraction
71     # !!! Note total shrink percentage is halved (width of band is percentage/2)
72     maxDelta = max(deltaWidth, deltaHeight) / 2 
73     
74     pdb.gimp_selection_shrink(image, maxDelta)
75     #if not pdb.gimp_selection_shrink(image, maxDelta):
76     #    raise RuntimeError,  "Failed shrink selection"
77
78
79 def uncrop(orgImage, drawable, percentEnlargeParam=10):
80     '''
81     Create frisket stencil selection in a temp image to pass as source (corpus) to plugin resynthesizer,
82     which does the substantive work.
83     '''
84     
85     if not pdb.gimp_item_is_layer(drawable):
86       pdb.gimp_message(_("A layer must be active, not a channel."))
87       return
88       
89     pdb.gimp_image_undo_group_start(orgImage)
90     
91     # copy original into temp for later use
92     tempImage = pdb.gimp_image_duplicate(orgImage)
93     if not tempImage:
94         raise RuntimeError, "Failed duplicate image"
95     
96     '''
97     Prepare target: enlarge canvas and select the new, blank outer ring
98     '''
99     
100     # Save original bounds to later select outer band
101     pdb.gimp_selection_all(orgImage)
102     selectAllPrior = pdb.gimp_selection_save(orgImage)
103     # Resize image alone doesn't resize layer, so resize layer also
104     resizeImageCentered(orgImage, percentEnlargeParam)
105     pdb.gimp_layer_resize_to_image_size(drawable)
106     pdb.gimp_image_select_item(orgImage, CHANNEL_OP_REPLACE, selectAllPrior)
107     # select outer band, the new blank canvas.
108     pdb.gimp_selection_invert(orgImage)
109     # Assert target image is ready.
110                   
111     '''
112     Prepare source (corpus) layer, a band at edge of original, in a dupe.
113     Note the width of corpus band is same as width of enlargement band.
114     '''
115     # Working with the original size.
116     # Could be alpha channel transparency
117     workLayer = pdb.gimp_image_get_active_layer(tempImage)
118     if not workLayer:
119         raise RuntimeError, "Failed get active layer"
120     # Select outer band:  select all, shrink
121     pdb.gimp_selection_all(tempImage)
122     shrinkSelectionByPercent(tempImage, percentEnlargeParam)
123     pdb.gimp_selection_invert(tempImage)    # invert interior selection into a frisket
124     # Note that v1 resynthesizer required an inverted selection !!
125     # No need to crop corpus to save memory.
126       
127     # Note that the API hasn't changed but use_border param now has more values.
128     # !!! The crux: use_border param=5 means inside out direction
129     pdb.plug_in_resynthesizer(orgImage, drawable, 0,0,5, workLayer.ID, -1, -1, 0.0, 0.117, 16, 500)
130     
131     # Clean up. 
132     # Any errors now are moot.
133     pdb.gimp_selection_none(orgImage)
134     pdb.gimp_image_remove_channel(orgImage, selectAllPrior)
135     pdb.gimp_image_undo_group_end(orgImage)
136     pdb.gimp_displays_flush()
137     gimp.delete(tempImage)  # Comment out to debug corpus creation.
138
139
140 register(
141   "python_fu_uncrop",
142   N_("Enlarge image by synthesizing a border that matches the edge, maintaining perspective.  Works best for small enlargement of natural edges. Undo a Crop instead, if possible! "),
143   "Requires separate resynthesizer plugin.",
144   "Lloyd Konneker",
145   "Copyright 2009 Lloyd Konneker",
146   "2009",
147   N_("Uncrop..."),
148   "RGB*, GRAY*",
149   [
150     (PF_IMAGE, "image",       "Input image", None),
151     (PF_DRAWABLE, "drawable", "Input drawable", None),
152     (PF_SLIDER, "percentEnlargeParam", _("Percent enlargement"), 10, (0, 100, 1))
153   ],
154   [],
155   uncrop,
156   menu="<Image>/Filters/Enhance",
157   domain=("resynthesizer", gimp.locale_directory)
158   )
159
160 main()
161