Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # This is a GIMP script written in Python for putting an outline around text (or indeed anything
  4. # else).
  5. #
  6. # To use this script, make sure that the selected layer is the one that you want to outline, then
  7. # choose "Text Outliner" from the "Layer" menu.
  8. #
  9. # You will then be able to set various parameters for the outline. They are:
  10. #       Thickness - thickness of the outline (in pixels)
  11. #       Feather - how soft you want the edge of the outline to be
  12. #
  13. # The script will create a new, transparent layer underneath your selected layer filled with
  14. # the background color. Then the text layer will merge down onto the outline layer and will
  15. # add a new animstack tag with the original layer name.
  16.  
  17. from gimpfu import *
  18.  
  19. # Adds a new layer beneath the given layer. Return value is the new layer
  20. def add_new_layer_beneath(image, layer):
  21.     # Get the layer position.
  22.     pos = 0;
  23.     for l in image.layers:
  24.         if (l == layer):
  25.             break
  26.         else:
  27.             pos += 1
  28.    
  29.     if image.base_type is RGB:
  30.         type = RGBA_IMAGE
  31.     else:
  32.         type = GRAYA_IMAGE
  33.        
  34.     # Add a new layer below the selected one
  35.     title = layer.name + ' [fg>' + layer.name + ']'
  36.     new_layer = gimp.Layer(image, title, image.width, image.height, type, 100, NORMAL_MODE)
  37.     image.add_layer(new_layer, pos+1)  
  38.     return new_layer
  39.  
  40. # Selects the contents of the given layer, then grows it by "thickness"
  41. # and feathers it by "feather" pixels
  42. def create_selection(image, layer, thickness, feather):
  43.     # Select the text
  44.     pdb.gimp_selection_layer_alpha(layer)
  45.    
  46.     # Grow the selection
  47.     pdb.gimp_selection_grow(image, thickness)
  48.    
  49.     # Feather it
  50.     if (feather > 0):
  51.         pdb.gimp_selection_feather(image, feather)     
  52.     return
  53.  
  54. # Fills the current selection using the given colour, painting onto the
  55. # given layer.
  56. def fill_selection(layer):
  57.     # Fill the selection using the background color
  58.     pdb.gimp_bucket_fill(layer, 1, 0, 100, 0, 0, 1, 1) 
  59.     return
  60.  
  61. def merge_down(image, layer):
  62.     # Merge the existing layer down. Second argument is GimpMergeType:
  63.     # https://developer.gimp.org/api/2.0/libgimp/libgimp-gimpenums.html#GimpMergeType
  64.     image.merge_down(layer, 1);  # Clip to image
  65.     return
  66.  
  67. # our script
  68. def add_text_outline(image, layer, thickness, feather) :
  69.     gimp.progress_init("Drawing outline around text")
  70.     new_layer = add_new_layer_beneath(image, layer)
  71.     gimp.progress_update(33)
  72.     create_selection(image, layer, thickness, feather)
  73.     gimp.progress_update(66)
  74.     fill_selection(new_layer)
  75.     merge_down(image, layer)   
  76.     gimp.progress_update(100)
  77.     return
  78.  
  79. # This is the plugin registration function
  80. register(
  81.     "text_outliner",   
  82.     "Text Outliner",  
  83.     "Will draw an outline around text using the background color.",
  84.     "Pete Nu",
  85.     "Pete Nu",
  86.     "Feb 2015",
  87.     "<Image>/Layer/Text Outline",
  88.     "*",
  89.     [
  90.         (PF_INT, 'outline_thickness', 'Thickness', 6),
  91.         (PF_INT, 'outline_featheriness', 'Feather', 7)
  92.     ],
  93.     [],
  94.     add_text_outline)
  95.  
  96. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement