Giant blob of minor changes
[dotfiles/.git] / .config / GIMP / 2.10 / plug-ins / plugin-heal-transparency.py
1 #!/usr/bin/env python
2
3 '''
4 Gimp plugin "Heal transparency"
5
6 Copyright 2010 lloyd konneker (bootch at nc.rr.com)
7
8 Version:
9   1.0 lloyd konneker lkk 2010 Initial version in python.
10
11 License:
12
13   This program is free software; you can redistribute it and/or modify
14   it under the terms of the GNU General Public License as published by
15   the Free Software Foundation; either version 2 of the License, or
16   (at your option) any later version.
17
18   This program is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21   GNU General Public License for more details.
22
23   The GNU Public License is available at
24   http://www.gnu.org/copyleft/gpl.html
25   
26 '''
27
28 from gimpfu import *
29
30 gettext.install("resynthesizer", gimp.locale_directory, unicode=True)
31
32 def heal_transparency(timg, tdrawable, samplingRadiusParam=50, orderParam=2):
33
34   # precondition should be enforced by Gimp according to image modes allowed.
35   if not pdb.gimp_drawable_has_alpha(tdrawable):
36     pdb.gimp_message("The active layer has no alpha channel to heal.")
37     return
38   
39   pdb.gimp_image_undo_group_start(timg)
40   
41   # save selection for later restoration.
42   # Saving selection channel makes it active, so we must save and restore the active layer
43   org_selection = pdb.gimp_selection_save(timg)
44   pdb.gimp_image_set_active_layer(timg, tdrawable)
45   
46   # alpha to selection
47   pdb.gimp_image_select_item(timg, CHANNEL_OP_REPLACE, tdrawable)
48   # Want the transparent, not the opaque.  
49   pdb.gimp_selection_invert(timg)
50   # Since transparency was probably anti-aliased (dithered with partial transpancy),
51   # grow the selection to get past the dithering.
52   pdb.gimp_selection_grow(timg, 1)
53   # Remove the alpha from this layer. IE compose with current background color (often white.)
54   # Resynthesizer won't heal transparent.
55   pdb.gimp_layer_flatten(tdrawable)
56   
57   # Call heal selection (not the resynthesizer), which will create a proper corpus.
58   # 0 = sample from all around
59   pdb.python_fu_heal_selection(timg, tdrawable, samplingRadiusParam, 0, orderParam, run_mode=RUN_NONINTERACTIVE)
60  
61   # Restore image to initial conditions of user, except for later cleanup.
62   
63   # restore selection
64   pdb.gimp_image_select_item(timg, CHANNEL_OP_REPLACE, org_selection)
65  
66   # Clean up (comment out to debug)
67   pdb.gimp_image_undo_group_end(timg)
68
69
70 register(
71   "python_fu_heal_transparency",
72   N_("Removes alpha channel by synthesis.  Fill outward for edges, inward for holes."),
73   "Requires separate resynthesizer plugin.",
74   "Lloyd Konneker",
75   "Copyright 2010 Lloyd Konneker",
76   "2010",
77   N_("Heal transparency..."),
78   "RGBA, GRAYA",  # !!! Requires an alpha channel to heal
79   [
80     (PF_IMAGE, "image",       "Input image", None),
81     (PF_DRAWABLE, "drawable", "Input drawable", None),
82     (PF_INT, "samplingRadiusParam", _("Context sampling width (pixels):"), 50),
83     (PF_OPTION, "orderParam",   _("Filling order:"), 2, [_("Random"),
84       _("Inwards towards center"), _("Outwards from center") ])
85   ],
86   [],
87   heal_transparency,
88   menu="<Image>/Filters/Enhance",
89   domain=("resynthesizer", gimp.locale_directory)
90   )
91
92 main()