Giant blob of minor changes
[dotfiles/.git] / .config / GIMP / 2.10 / plug-ins / plugin-resynth-fill-pattern.py
1 #!/usr/bin/env python
2
3 '''
4 Gimp plugin "Fill with pattern seamless..."
5 Front end to the resynthesizer plugin to make a seamless fill.
6
7 Copyright 2011 lloyd konneker
8 Idea by Rob Antonishen
9
10 Version:
11 1.0 lloyd konneker
12
13 During development, remember to make it executable!!!
14 And to remove older versions, both .scm and .py that might hide it.
15
16 License:
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License for more details.
27
28 The GNU Public License is available at
29 http://www.gnu.org/copyleft/gpl.html
30
31 '''
32
33 from gimpfu import *
34
35 gettext.install("resynthesizer", gimp.locale_directory, unicode=True);
36
37 debug = False
38
39 def layer_from_pattern(image, pattern):
40   '''
41   Create a new image and layer having the same size as a pattern.
42   '''
43   new_basetype = pdb.gimp_image_base_type(image)  # same as source
44   new_layertype = pdb.gimp_drawable_type(pdb.gimp_image_get_active_layer(image))
45   pattern_width, pattern_height, bpp = pdb.gimp_pattern_get_info(pattern)
46   new_image = pdb.gimp_image_new(pattern_width, pattern_height, new_basetype)
47   # !!! Note that gimp_layer_new wants a layer type, not an image basetype
48   new_drawable = pdb.gimp_layer_new(new_image, pattern_width, pattern_height,
49     new_layertype, "Texture", 100, NORMAL_MODE)
50   pdb.gimp_image_add_layer(new_image, new_drawable, 0)
51   return new_image, new_drawable
52   
53   
54 def guts(image, drawable, pattern):
55   ''' Crux of algorithm '''
56   
57   # Make drawble from pattern
58   pattern_image, pattern_layer = layer_from_pattern(image, pattern)
59   
60   # Fill it with pattern
61   # NOT pass pattern_layer.ID !!!
62   pdb.gimp_drawable_fill(pattern_layer, PATTERN_FILL)
63   
64   if debug:
65     gimp.Display(pattern_image) 
66     gimp.displays_flush() 
67   
68   # Resynthesize the selection from the pattern without using context
69   # 0,0,0: Not use_border (context), not tile horiz, not tile vert
70   # -1, -1, 0: No maps and no map weight
71   # DO pass pattern_layer.ID !!!
72   # Resynthesizer is an engine, never interactive
73   pdb.plug_in_resynthesizer(image, drawable, 0, 0, 0, pattern_layer.ID, -1, -1, 0, 0.05, 8, 300)
74       
75   # Clean up
76   if not debug:
77     # Delete image that is displayed throws RuntimeError
78     pdb.gimp_image_delete(pattern_image)
79   
80   
81 def plugin_main(image, drawable, pattern):
82   '''
83   Main: the usual user-friendly precondition checking, postcondition cleanup.
84   pattern is a string
85   '''  
86   # User_friendly: if no selection, use entire image.
87   # But the resynthesizer does that for us.
88   
89   # Save/restore the context since we change the pattern
90   pdb.gimp_context_push()
91   pdb.gimp_context_set_pattern(pattern)
92   guts(image, drawable, pattern)
93   pdb.gimp_context_pop()
94   
95   gimp.displays_flush()
96
97
98 register(
99   "python_fu_fill_pattern_resynth",
100   N_("Seamlessly fill with a pattern using synthesis."),
101   "Requires separate resynthesizer plugin.",
102   "Lloyd Konneker",
103   "Copyright 2011 Lloyd Konneker",
104   "2011",
105   N_("_Fill with pattern seamless..."),
106   "RGB*, GRAY*",
107   [
108     (PF_IMAGE,    "image",    "Input image", None),
109     (PF_DRAWABLE, "drawable", "Input drawable", None),
110     (PF_PATTERN,  "pattern",  _("Pattern:"), 'Maple Leaves')
111   ],
112   [],
113   plugin_main,
114   menu="<Image>/Edit",
115   domain=("resynthesizer", gimp.locale_directory)
116   )
117
118 main()