#!/usr/bin/env python ''' Gimp plugin. Transfer style (color and surface texture) from a source image to the active, target image. Requires resynthesizer plug-in. Author: lloyd konneker, lkk, bootch at nc.rr.com Version: 1.0 lkk 7/15/2010 Initial version License: This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU Public License is available at http://www.gnu.org/copyleft/gpl.html Users Guide ___________ What this plugin does: 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. 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." Transferring style means transferring small scale features (color and texture) to an image while retaining large scale features (objects.) 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.) 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. 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. The transfer is not exactly a copy, again because of optional synthesis or mixing. About the selection: 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. 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. 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. About the settings: "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.) "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. 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. About modes: 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. Why this plugin: 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. Programming notes: _________________ IN: The active image and layer. The selection in the active image. The selection in any layers chosen for source. OUT: The active image, altered. The source is unaltered. Target mode can be altered, but with the implied consent of the user. The print stmts go to the console, info to advanced users and debuggers. This plugin is mostly about UI and simplifications for user (the engine does the image processing): making maps automatically synchronization of alphas (note the resynthesizer SHOULD be changed to ignore alphas.) synchronization of modes abstracting the settings contrast adjustment ''' from gimpfu import * from math import acos # True if you want to display and retain working, temporary images debug = False def display_debug_image(image) : if debug : pdb.gimp_display_new(image) pdb.gimp_displays_flush() def make_grayscale_map(image, drawable): ''' Make a grayscale copy for a map. Maps must be same size as their parent image. If image is already grayscale, return it without copying. Maps don't need a selection, since the resynthesizer looks at parent drawables for the selection. ''' if pdb.gimp_image_base_type(image) == GRAY : return image, drawable # Save selection, copy entire image, and restore original_selection = pdb.gimp_selection_save(image) pdb.gimp_selection_all(image) # copy requires selection pdb.gimp_edit_copy(drawable) if original_selection : pdb.gimp_selection_load(original_selection) # restore selection in image # Make a copy, greyscale temp_image = pdb.gimp_edit_paste_as_new() pdb.gimp_image_flatten(temp_image) # !!! to activate layer pdb.gimp_image_convert_grayscale(temp_image) display_debug_image(temp_image) temp_drawable = pdb.gimp_image_get_active_drawable(temp_image) return temp_image, temp_drawable def synchronize_modes(target_image, source_image) : ''' User-friendliness: If mode of target is not equal to mode of source source, change modes. Resynthesizer requires target and source to be same mode. Assert target is RGB or GRAY (since is precondition of plugin.) UI decision: make this quiet, presume user intends mode change. But don't permanently change mode of source. ''' target_mode = pdb.gimp_image_base_type(target_image) source_mode = pdb.gimp_image_base_type(source_image) if target_mode != source_mode : print("Map style: converted mode\n.") if target_mode == GRAY: pdb.gimp_image_convert_rgb(target_image) else : # target is RGB and source is GRAY # Assert only convert a copy of source, # user NEVER intends original source be altered. pdb.gimp_image_convert_rgb(source_image) def synchronize_alphas(target_drawable, source_drawable) : ''' User-friendliness: If source has alpha and target doesn't, remove or add alpha to source. Do this without user dialog since it is done on copies, and really, the alpha doesn't matter. TODO Best if resynthesizer changed so it doesn't care. ''' if pdb.gimp_drawable_has_alpha(source_drawable) : if not pdb.gimp_drawable_has_alpha(target_drawable) : # Should never get here, since removed alpha from source_drawable copy earlier print "Adding alpha channel to target image since style source image has alpha." pdb.gimp_layer_add_alpha (target_drawable) else: # source has no alpha if pdb.gimp_drawable_has_alpha(target_drawable) : print "Map style: Adding alpha channel to style source image copy since target image has alpha." pdb.gimp_layer_add_alpha (source_drawable) def copy_selection_to_image(drawable) : ''' If image has a selection, copy selection to new image, and prepare it for resynthesizer, else return a copy of the entire source image. This is called for the source image, where it helps performance to reduce size and flatten. Also, if there is a selection, the resynthesizer wants it inverted. ''' image = pdb.gimp_drawable_get_image(drawable) # copy selection or whole image pdb.gimp_edit_copy(drawable) image_copy = pdb.gimp_edit_paste_as_new() layer_copy = pdb.gimp_image_get_active_layer(image_copy) (is_selection, ulx, uly, lrx, lry) = pdb.gimp_selection_bounds(image) if is_selection : # Assert copy is size of the original selection, but might have transparency. # Assert copy does have alpha channel (but might be all visible) # Therefore because of a bug in the resynthesizer engine, we need to select the non-transparent. pdb.gimp_selection_layer_alpha(layer_copy) # make selection from the alpha # Old engine wants an inverted selection pdb.gimp_selection_invert(image_copy) else : # Assert image has no selection, engine will use the whole active layer pass # Remove alpha layer from the copy. # If target has an alpha, we will add alpha back to source copy soon. pdb.gimp_layer_flatten(layer_copy) display_debug_image(image_copy) return image_copy, layer_copy def synchronize_contrast( drawable, source_drawable, percent_transfer) : ''' Adjust contrast of source, to match target. Adjustment depends inversely on percent_transfer. Very crude histogram matching. ''' # histogram upper half: typical mean is 191 (3/4*255). Skew of mean towards 255 means high contrast. mean, deviation, median, pixels, count, percentile = pdb.gimp_histogram(drawable, HISTOGRAM_VALUE, 128, 255) source_mean, source_deviation, source_median, pixels, count, percentile = pdb.gimp_histogram( source_drawable, HISTOGRAM_VALUE, 128, 255) # if mean > source_mean: # target has more contrast than source # Adjust contrast of source. # Inversely proportional to percent transfer. # 2.5 is from experimentation with gimp_brightness_contrast which seems linear in its effect. contrast_control = (mean - source_mean) * 2.5 * (1 - (percent_transfer / 100)) # clamp to valid range (above formula is lazy, ad hoc) if contrast_control < -127: contrast_control = -127 if contrast_control > 127: contrast_control = 127 pdb.gimp_brightness_contrast(source_drawable, 0, contrast_control) # For experimentation, print new values source_mean, source_deviation, source_median, pixels, count, percentile = pdb.gimp_histogram( source_drawable, HISTOGRAM_VALUE, 128, 255) print "Map style: Source contrast changed by ", contrast_control print "Map style: Target and source upper half histogram means", mean, source_mean def calculate_map_weight(percent_transfer) : ''' This is a GUI design discussion. Transform percent_transfer to map_weight parameter to resynthesizer. For resynthesizer: map weight 0 means copy source to target, meaning ALL style. map weight 0.5 means just a grainy transfer of style (as little as is possible.) Transform from a linear percent GUI, because user more comfortable than with a ratio [.5, 0] which is backwards to the usual *less on the left*. By experiment, a sinusoid gives good results for linearizing the non-linear map_weight control. ''' return acos((percent_transfer/100)*2 -1)/(2*3.14) def transfer_style(image, drawable, source_drawable, percent_transfer, mode ): ''' Main body of plugin to transfer style from one image to another. ''' pdb.gimp_image_undo_group_start(image) # Get image of source drawable source_image = pdb.gimp_drawable_get_image(source_drawable) ''' User-friendliness. Note the drawable chooser widget in Pygimp does not allow us to prefilter INDEXED mode. So check here and give a warning. ''' source_base_type = pdb.gimp_image_base_type(source_image) target_base_type = pdb.gimp_image_base_type(image) if source_base_type == INDEXED : pdb.gimp_message("The style source cannot be of mode INDEXED"); return ''' If source is same as target, then MUST be a selection (engine uses selection for corpus) and there is no need to copy source. ''' if image == source_image and drawable == source_drawable: is_source_copy = False (is_selection, ulx, uly, lrx, lry) = pdb.gimp_selection_bounds(image) if not is_selection : pdb.gimp_message("Source layer cannot be the same as the active layer unless there is a selection.") return # assert modes and alphas are same (since they are same layer!) else: # target layer is not the source layer (source could be a copy of target, but effect is none) # Copy source always, for performance, and for possible mode change. is_source_copy = True source_image, source_drawable = copy_selection_to_image(source_drawable) # Futz with modes if necessary. synchronize_modes(image, source_image) # User-friendliness: If source has alpha and target doesn't, remove or add alpha. synchronize_alphas( drawable, source_drawable) ''' TODO For performance, if there is a selection in target, it would be better to copy selection to a new layer, and later merge it back (since resynthesizer engine reads entire target into memory. Low priority since rarely does user make a selection in target. ''' # For style transfer, maps start the same mode as drawable. # !!! Note there are always maps to the resynthesizer, and the "percent transfer" setting is always effective. # However, maps may not be separate images unless converted to grayscale, see below. source_map = source_drawable target_map = drawable # Reduce maps to grayscale: at the option of the user # !!! Or if the target is gray and source is RGB, in which case maps give a better result if mode or (source_base_type == RGB and target_base_type == GRAY) : print "Map style: Converting maps to grayscale" # Convert mode, but in new temp image and drawable target_map_image, target_map_drawable = make_grayscale_map(image, drawable) source_map_image, source_map_drawable = make_grayscale_map(source_image, source_drawable) target_map = target_map_drawable source_map = source_map_drawable # later, delete temp images # User control: adjust contrast of source_map as a function of percent transfer # Hard to explain why, but experimentation shows result more like user expectation. # TODO This could be improved. synchronize_contrast( drawable, source_map, percent_transfer) ''' Parameters to resynthesizer: htile and vtile = 1 since it reduces artifacts around edge map_weight I linearize since easier on users than an exponential use_border = 1 since there might be a selection and context (outside target). 9 neighbors (a 3x3 patch) and 200 tries for speed TODO a quality setting ''' map_weight = calculate_map_weight(percent_transfer) print "Map style: Map weight to resynthesizer", map_weight # !!! The crux: call resynthesizer # !!! This is for a test version of resynthesizer, with an uninverted selection # pdb.plug_in_resynthesizer2(image, drawable, 1, 1, 1, source_drawable, source_map, target_map, map_weight, 0.117, 9, 200) # !!! This is for the current resynthesizer, which needs image.ID and an inverted selection pdb.plug_in_resynthesizer(image, drawable, 1, 1, 1, source_drawable.ID, source_map.ID, target_map.ID, map_weight, 0.117, 9, 200) # Clean up. # Delete working images: separate map images and copy of source image if not debug: if mode: # if made working map images pdb.gimp_image_delete(target_map_image) pdb.gimp_image_delete(source_map_image) if is_source_copy: # if created a copy earlier pdb.gimp_image_delete(source_image) pdb.gimp_image_undo_group_end(image) pdb.gimp_displays_flush() register( "python_fu_map_style", "Transfer style (color and surface) from a chosen source to the active layer. Requires separate resynthesizer plug-in.", "Transforms image using artistic media and style from another image. Maps or synthesizes texture or theme from one image onto another.", "Lloyd Konneker (bootch nc.rr.com)", "Copyright 2010 Lloyd Konneker", "2010", "/Filters/Map/_Style...", "RGB*, GRAY*", [ (PF_DRAWABLE, "source_drawable", "Source of style:", None), (PF_SLIDER, "percent_transfer", "Percent transfer:", 0, (10, 90, 10.0)), (PF_RADIO, "mode", "Map by:", 0, (("Color and brightness", 0),("Brightness only",1))) ], [], transfer_style ) main()