5 Transfer style (color and surface texture) from a source image to the active, target image.
7 Requires resynthesizer plug-in.
13 1.0 lkk 7/15/2010 Initial version. Released to Gimp Registry.
14 1.1 lkk 8/1/2010 Unreleased
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
23 Changes for new resynthesizer: no need to synchronize, remove alphas
24 Fixed improper adjustment of contrast of source: only adjust source map.
27 a quality setting that changes the parameters to resynth
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.
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.
41 The GNU Public License is available at
42 http://www.gnu.org/copyleft/gpl.html
48 What this plugin does:
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.
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."
54 Transferring style means transferring small scale features (color and texture) to an image while retaining large scale features (objects.)
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.)
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.
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.
62 The transfer is not exactly a copy, again because of optional synthesis or mixing.
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.
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.
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.
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.)
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.
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.
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.
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.
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.
99 The print stmts go to the console, info to advanced users and debuggers.
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
111 from math import acos
113 gettext.install("resynthesizer", gimp.locale_directory, unicode=True)
115 # True if you want to display and retain working, temporary images
118 def display_debug_image(image) :
121 pdb.gimp_display_new(image)
122 pdb.gimp_displays_flush()
124 pass # if run-mode not interactive, Gimp throws
127 def make_grayscale_map(image, drawable):
129 Make a grayscale copy for a map.
131 Maps must be same size as their parent image.
133 If image is already grayscale, return it without copying.
135 Maps don't need a selection, since the resynthesizer looks at parent drawables for the selection.
137 if pdb.gimp_image_base_type(image) == GRAY :
138 return image, drawable
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
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
157 def synchronize_modes(target_image, source_image) :
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.
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)
182 def synchronize_alphas(target_drawable, source_drawable) :
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.
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)
200 def copy_selection_to_image(drawable) :
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.
206 image = pdb.gimp_drawable_get_image(drawable)
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
219 def synchronize_contrast( drawable, source_drawable, percent_transfer) :
221 Adjust contrast of source, to match target.
222 Adjustment depends inversely on percent_transfer.
223 Very crude histogram matching.
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
245 def calculate_map_weight(percent_transfer) :
247 This is a GUI design discussion.
248 Transform percent_transfer to map_weight parameter to 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.
256 return acos((percent_transfer/100)*2 -1)/(2*3.14)
259 def transfer_style(image, drawable, source_drawable, percent_transfer, map_mode ):
261 Main body of plugin to transfer style from one image to another.
263 !!! Note map_mode is type string, "if map_mode:" will not work.
266 pdb.gimp_image_undo_group_start(image)
268 # Get image of source drawable
269 source_image = pdb.gimp_drawable_get_image(source_drawable)
273 Note the drawable chooser widget in Pygimp does not allow us to prefilter INDEXED mode.
274 So check here and give a warning.
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)
280 if original_source_base_type == INDEXED :
281 pdb.gimp_message(_("The style source cannot be of mode INDEXED"));
284 if image == source_image and drawable == source_drawable:
285 is_source_copy = False
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.
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)
298 # Futz with modes if necessary.
299 synchronize_modes(image, source_image)
302 Old resythesizer required both images to have alpha, or neither.
303 synchronize_alphas( drawable, source_drawable)
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.
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.
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)
328 target_map = target_map_drawable
329 source_map = source_map_drawable
330 # later, delete temp images
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)
338 # !!! Maps ARE the target and source, not copies
339 source_map = source_drawable
340 target_map = drawable
344 Parameters to resynthesizer:
346 htile and vtile = 1 since it reduces artifacts around edge
348 map_weight I linearize since easier on users than an exponential
350 use_border = 1 since there might be a selection and context (outside target).
352 9 neighbors (a 3x3 patch) and 200 tries for speed
356 map_weight = calculate_map_weight(percent_transfer)
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)
362 # Delete working images: separate map images and copy of source image
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)
370 pdb.gimp_image_undo_group_end(image)
371 pdb.gimp_displays_flush()
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",
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)))
392 menu="<Image>/Filters/Map",
393 domain=("resynthesizer", gimp.locale_directory)