Giant blob of minor changes
[dotfiles/.git] / .config / GIMP / 2.10 / plug-ins / plugin-map-style.py
1 #!/usr/bin/env python
2
3 '''
4 Gimp plugin.
5 Transfer style (color and surface texture) from a source image to the active, target image.
6
7 Requires resynthesizer plug-in.
8
9 Author:
10 lloyd konneker, lkk
11
12 Version:
13 1.0 lkk 7/15/2010 Initial version.  Released to Gimp Registry.
14 1.1 lkk 8/1/2010 Unreleased
15 1.2 lkk 8/10/2010 
16
17 Change log:
18 _________________
19 1.1 
20   Bug: Fixed test of mode variable, since it is a string, needs explicit test for == 1
21   Bug: Added remove Selection Mask copy channel in make_grayscale_map
22 1.2
23   Changes for new resynthesizer: no need to synchronize, remove alphas
24   Fixed improper adjustment of contrast of source: only adjust source map.
25
26 TODO
27 a quality setting that changes the parameters to resynth
28
29 License:
30
31 This program is free software; you can redistribute it and/or modify
32 it under the terms of the GNU General Public License as published by
33 the Free Software Foundation; either version 2 of the License, or
34 (at your option) any later version.
35
36 This program is distributed in the hope that it will be useful,
37 but WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39 GNU General Public License for more details.
40
41 The GNU Public License is available at
42 http://www.gnu.org/copyleft/gpl.html
43
44
45 Users Guide
46 ___________
47
48 What this plugin does:
49
50 Transfers artistic style from one image to another.  Often the source is an artistic image and the target is a realistic, photo image.  But you can also transfer between artistic images or between realistic images.
51
52 An artist might say this plugin "renders in the media and style from another image."  A computer user might say it "renders in the theme of another image."
53
54 Transferring style means transferring small scale features (color and texture) to an image while retaining large scale features (objects.)
55
56 Map can mean "transfer and transform".  This plugin gives limited control of the transform.  That is, colors are usually mapped to similar colors (hues.)  This plugin is not intended to do "false color" (but it might have that effect.)
57
58 Style can mean "color and surface."  Texture mapping usually means just surface (pattern of brightness, e.g. a weave or grain.)  This plugin can transfer both color and surface.
59
60 This plugin has more effect than just an overlay or screen or a map.  A screen usually applies a texture uniformly across an image.  This plugin transfers style in patches.  The style in a region can come from any patch of the source, or be synthesized (mixed) from many patches of the source.
61
62 The transfer is not exactly a copy, again because of optional synthesis or mixing.
63
64 About the selection:
65
66 Usually you transfer between separate images, the target and source images.  You can make a selection in either image, or both.  If there is no selection, the plugin uses the entire layer.
67
68 The target is the active LAYER and you can choose the source LAYER.  Note that the plugin doesn't use everything visible in an image, just one layer.
69
70 SPECIAL CASE: If the target and source layers are in the same image, the source style comes from the inverse of the selection in the source layer.  Similarly, if the target and source layers are the same layer, the target is the selection and the style comes from the inverse of the selection, i.e. outside the selection.  In this case, the effect is little if there is no difference in texture between the inside and outside of the selection, or a distort, if there is a difference.
71
72 About the settings:
73
74 "Percent transfer:" how much style to transfer.  Less transfer means the effect retains the large scale objects of the original, but gives the image a grainy surface.  More transfer means the effect leaves only a ghost of the large scale objects, and almost fully copies the style image (with less synthesis or mixing.)
75
76 "Map by:" whether color affects the style transfer, when both target and source are in color.  If you choose "color and brightness", style colors are more apt to be transferred to areas with same colors.  However, it is still possible that colors are radically transformed, if the surface (brightness pattern) is a better match.  If you choose "brightness only", style colors are more apt to be radically transformed.
77
78 This setting has less effect if there are no color matches between source and target (e.g. one is all red and the other is all green) or if the target image is GRAY.  This setting has NO effect if the source image or both images are GRAY.
79
80 About image modes:
81
82 You can transfer style between any combination of RGB and GRAY images.   The plugin changes the mode of the target to the mode of the source as necessary.
83
84 Why this plugin:
85
86 This plugin is a front-end to the separate resynthesizer plugin.  This plugin simplifies using the resynthesizer plugin.  It automates many steps.  It hides several complexities of the resynthesizer plugin:  selection, modes, alpha channels, and settings.
87
88
89
90 Programming notes:
91 _________________
92
93 IN: The active image and layer.
94     The selection in the active image.
95     The selection in any layers chosen for source.
96 OUT: The active image, altered.  The source is unaltered.  
97   Target mode can be altered, but with the implied consent of the user.
98
99 The print stmts go to the console, info to advanced users and debuggers.
100
101 This plugin is mostly about UI and simplifications for user (the engine does the image processing):
102 making maps automatically
103 synchronization of alphas (note the new resynthesizer ignores alphas.)
104 synchronization of modes
105 abstracting the settings
106 contrast adjustment
107
108 '''
109
110 from gimpfu import *
111 from math import acos
112
113 gettext.install("resynthesizer", gimp.locale_directory, unicode=True)
114
115 # True if you want to display and retain working, temporary images
116 debug = False
117
118 def display_debug_image(image) :
119   if debug :
120     try:
121       pdb.gimp_display_new(image)
122       pdb.gimp_displays_flush()
123     except RuntimeError:
124       pass    # if run-mode not interactive, Gimp throws
125
126
127 def make_grayscale_map(image, drawable):
128   '''
129   Make a grayscale copy for a map.
130   
131   Maps must be same size as their parent image.
132   
133   If image is already grayscale, return it without copying.
134   
135   Maps don't need a selection, since the resynthesizer looks at parent drawables for the selection.
136   '''
137   if pdb.gimp_image_base_type(image) == GRAY :
138     return image, drawable
139     
140   # Save selection, copy entire image, and restore
141   original_selection = pdb.gimp_selection_save(image)
142   pdb.gimp_selection_all(image) # copy requires selection
143   pdb.gimp_edit_copy(drawable)
144   if original_selection:
145     pdb.gimp_image_select_item(image, CHANNEL_OP_REPLACE, original_selection) # restore selection in image
146     pdb.gimp_image_remove_channel(image, original_selection) # cleanup the copied selection mask
147     # !!! Note remove_channel not drawable_delete
148   
149   # Make a copy, greyscale
150   temp_image = pdb.gimp_edit_paste_as_new()
151   pdb.gimp_image_convert_grayscale(temp_image)
152   display_debug_image(temp_image)
153   temp_drawable = pdb.gimp_image_get_active_drawable(temp_image)
154   return temp_image, temp_drawable
155
156
157 def synchronize_modes(target_image, source_image) :
158   '''
159   User-friendliness:
160   If mode of target is not equal to mode of source source, change modes.
161   Resynthesizer requires target and source to be same mode.
162   Assert target is RGB or GRAY (since is precondition of plugin.)
163   UI decision: make this quiet, presume user intends mode change.
164   But don't permanently change mode of source.
165   Always upgrade GRAY to RGB, not downgrade RGB to GRAY.
166   '''
167   target_mode = pdb.gimp_image_base_type(target_image)
168   source_mode = pdb.gimp_image_base_type(source_image)
169   if target_mode != source_mode :
170     # print("Map style: converted mode\n.")
171     if target_mode == GRAY:
172       pdb.gimp_image_convert_rgb(target_image)
173     else : # target is RGB and source is GRAY
174       # Assert only convert a copy of source,
175       # user NEVER intends original source be altered.
176       pdb.gimp_image_convert_rgb(source_image)
177
178 '''
179 Not used
180 '''
181 """
182 def synchronize_alphas(target_drawable, source_drawable) :
183   '''
184   User-friendliness:
185   If source has alpha and target doesn't, remove or add alpha to source.
186   Do this without user dialog since it is done on copies, and really, the alpha doesn't matter.
187   '''
188   if pdb.gimp_drawable_has_alpha(source_drawable) :
189     if not pdb.gimp_drawable_has_alpha(target_drawable) :
190       # Should never get here, since removed alpha from source_drawable copy earlier
191       print "Adding alpha channel to target image since style source image has alpha."
192       pdb.gimp_layer_add_alpha (target_drawable)
193   else: # source has no alpha
194     if pdb.gimp_drawable_has_alpha(target_drawable) :
195       print "Map style: Adding alpha channel to style source image copy since target image has alpha."
196       pdb.gimp_layer_add_alpha (source_drawable)
197 """      
198       
199
200 def copy_selection_to_image(drawable) :
201   '''
202   If image has a selection, copy selection to new image, and prepare it for resynthesizer,
203   else return a copy of the entire source image.
204   This is called for the source image, where it helps performance to reduce size and flatten.
205   '''
206   image = pdb.gimp_drawable_get_image(drawable)
207   
208   # copy selection or whole image
209   pdb.gimp_edit_copy(drawable)
210   image_copy = pdb.gimp_edit_paste_as_new()
211   # Activate layer, and remove alpha channel
212   pdb.gimp_image_flatten(image_copy)
213   layer_copy = pdb.gimp_image_get_active_layer(image_copy)
214   # In earlier version, futzed with selection to deal with transparencey
215   display_debug_image(image_copy)
216   return image_copy, layer_copy
217     
218
219 def synchronize_contrast( drawable, source_drawable, percent_transfer) :
220   '''
221   Adjust contrast of source, to match target.
222   Adjustment depends inversely on percent_transfer.
223   Very crude histogram matching.
224   '''
225   # histogram upper half: typical mean is 191 (3/4*255). Skew of mean towards 255 means high contrast.
226   mean, deviation, median, pixels, count, percentile = pdb.gimp_histogram(drawable, HISTOGRAM_VALUE, 128, 255)
227   source_mean, source_deviation, source_median, pixels, count, percentile = pdb.gimp_histogram(
228     source_drawable, HISTOGRAM_VALUE, 128, 255)
229   # if mean > source_mean:  # target has more contrast than source
230   # Adjust contrast of source.
231   # Inversely proportional to percent transfer.
232   # 2.5 is from experimentation with gimp_brightness_contrast which seems linear in its effect.
233   contrast_control = (mean - source_mean) * 2.5 * (1 - (percent_transfer / 100))
234   # clamp to valid range (above formula is lazy, ad hoc)
235   if contrast_control < -127: contrast_control = -127
236   if contrast_control > 127: contrast_control = 127
237   pdb.gimp_brightness_contrast(source_drawable, 0, contrast_control)
238   # For experimentation, print new values
239   source_mean, source_deviation, source_median, pixels, count, percentile = pdb.gimp_histogram(
240     source_drawable, HISTOGRAM_VALUE, 128, 255)
241   # print "Map style: Source contrast changed by ", contrast_control
242   # print "Map style: Target and source upper half histogram means", mean, source_mean
243
244
245 def calculate_map_weight(percent_transfer) :
246   '''
247   This is a GUI design discussion.
248   Transform percent_transfer to map_weight parameter to resynthesizer.
249   For resynthesizer:
250   map weight 0 means copy source to target, meaning ALL style.
251   map weight 0.5 means just a grainy transfer of style (as little as is possible.)
252   Transform from a linear percent GUI, because user more comfortable than with a ratio [.5, 0]
253   which is backwards to the usual *less on the left*.
254   By experiment, a sinusoid gives good results for linearizing the non-linear map_weight control.
255   '''
256   return acos((percent_transfer/100)*2 -1)/(2*3.14)
257   
258
259 def transfer_style(image, drawable, source_drawable, percent_transfer, map_mode ):
260   '''
261   Main body of plugin to transfer style from one image to another.
262   
263   !!! Note map_mode is type string, "if map_mode:" will not work.
264   '''
265   
266   pdb.gimp_image_undo_group_start(image)
267   
268   # Get image of source drawable
269   source_image = pdb.gimp_drawable_get_image(source_drawable)
270   
271   ''' 
272   User-friendliness.
273   Note the drawable chooser widget in Pygimp does not allow us to prefilter INDEXED mode.
274   So check here and give a warning.
275   '''
276   # These are the originals base types, and this plugin might change the base types
277   original_source_base_type = pdb.gimp_image_base_type(source_image)
278   original_target_base_type = pdb.gimp_image_base_type(image)
279   
280   if original_source_base_type == INDEXED :
281     pdb.gimp_message(_("The style source cannot be of mode INDEXED"));
282     return
283
284   if image == source_image and drawable == source_drawable:
285     is_source_copy = False
286     '''
287     If source is same as target, 
288     then the old resynthesizer required a selection (engine used inverse selection for corpus).
289     New resynthesizer doesn't need a selection.
290     If source same as target, effect is similar to a blur.
291     '''
292     # assert modes and alphas are same (since they are same layer!)
293   else: # target layer is not the source layer (source could be a copy of target, but effect is none)
294     # Copy source always, for performance, and for possible mode change.
295     is_source_copy = True
296     source_image, source_drawable = copy_selection_to_image(source_drawable)
297     
298     # Futz with modes if necessary.
299     synchronize_modes(image, source_image)
300     
301     ''' 
302     Old resythesizer required both images to have alpha, or neither. 
303     synchronize_alphas( drawable, source_drawable)
304     '''
305
306   '''
307   TODO For performance, if there is a selection in target, it would be better to copy
308   selection to a new layer, and later merge it back (since resynthesizer engine reads
309   entire target into memory.  Low priority since rarely does user make a selection in target.
310   '''
311   
312   '''
313   !!! Note this plugin always sends maps to the resynthesizer, 
314   and the "percent transfer" setting is always effective.
315   However, maps may not be separate,copied images unless converted to grayscale.
316   '''
317   
318   # Copy and reduce maps to grayscale: at the option of the user
319   # !!! Or if the target was GRAY and source is RGB, in which case maps give a better result.
320   # Note that if the target was GRAY, we already upgraded it to RGB.
321   if map_mode == 1 or (original_source_base_type == RGB and original_target_base_type == GRAY) :
322     # print "Map style: source mode: ", original_source_base_type, " target mode: ", original_target_base_type
323     # print "Map style: Converting maps to grayscale"
324     # Convert mode, but in new temp image and drawable
325     target_map_image, target_map_drawable = make_grayscale_map(image, drawable)
326     source_map_image, source_map_drawable = make_grayscale_map(source_image, source_drawable)
327     
328     target_map = target_map_drawable
329     source_map = source_map_drawable
330     # later, delete temp images
331     
332     # User control: adjust contrast of source_map as a function of percent transfer
333     # Hard to explain why, but experimentation shows result more like user expectation.
334     # TODO This could be improved.
335     # !!! Don't change the original source, only a temporary map we created
336     synchronize_contrast( drawable, source_map, percent_transfer)
337   else :
338     # !!! Maps ARE the target and source, not copies
339     source_map = source_drawable
340     target_map = drawable
341
342     
343   '''
344   Parameters to resynthesizer:
345   
346   htile and vtile = 1 since it reduces artifacts around edge
347   
348   map_weight I linearize since easier on users than an exponential
349   
350   use_border = 1 since there might be a selection and context (outside target).
351   
352   9 neighbors (a 3x3 patch) and 200 tries for speed
353   
354   '''
355   
356   map_weight = calculate_map_weight(percent_transfer)
357     
358   # !!! This is for version of resynthesizer, with an uninverted selection
359   pdb.plug_in_resynthesizer(image, drawable, 1, 1, 1, source_drawable.ID, source_map.ID, target_map.ID, map_weight, 0.117, 9, 200)
360   
361   # Clean up.
362   # Delete working images: separate map images and copy of source image
363   if not debug:
364     if map_mode == 1:  # if made working map images
365       pdb.gimp_image_delete(target_map_image)
366       pdb.gimp_image_delete(source_map_image)
367     if is_source_copy:  # if created a copy earlier
368       pdb.gimp_image_delete(source_image)
369   
370   pdb.gimp_image_undo_group_end(image)
371   pdb.gimp_displays_flush()
372   
373
374 register(
375   "python_fu_map_style",
376   N_("Transfer style (color and surface) from a chosen source to the active layer. "),
377   "Transforms image using art media and style from another image.  Maps or synthesizes texture or theme from one image onto another. Requires separate resynthesizer plugin.",
378   "Lloyd Konneker (bootch nc.rr.com)",
379   "Copyright 2010 Lloyd Konneker",
380   "2010",
381   N_("Style..."),
382   "RGB*, GRAY*",
383   [
384     (PF_IMAGE, "image",       "Input image", None),
385     (PF_DRAWABLE, "drawable", "Input drawable", None),
386     (PF_DRAWABLE, "source_drawable", _("Source of style:"), None),
387     (PF_SLIDER, "percent_transfer", _("Percent transfer:"), 0, (10, 90, 10.0)),
388     (PF_RADIO, "map_mode", _("Map by:"), 0, ((_("Color and brightness"), 0),(_("Brightness only"),1)))
389   ],
390   [],
391   transfer_style,
392   menu="<Image>/Filters/Map",
393   domain=("resynthesizer", gimp.locale_directory)
394   )
395
396 main()
397