Advertisement
Guest User

Text with outline and blur (GIMP Python-Fu)

a guest
Nov 29th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. from gimpfu import *
  2.  
  3. def munge(img, s):
  4.     names = []
  5.     for layer in img.layers:
  6.         names.append(layer.name)
  7.     n = 0
  8.     s1 = s
  9.     while s1 in names:
  10.         s1 = '{}{}'.format(s, n)
  11.         n += 1
  12.     return s1
  13.  
  14. def do_stuff(img, layer, initstr, font, size, fgcolor, bgcolor, outline_radius, blur_radius):
  15.     gimp.progress_init("Doing stuff to " + layer.name + "...")
  16.  
  17.     # Set up an undo group, so the operation will be undone in one step.
  18.     pdb.gimp_undo_push_group_start(img)
  19.  
  20.     # Save the current foreground color:
  21.     pdb.gimp_context_push()
  22.  
  23.     # Set the text color
  24.     gimp.set_foreground(fgcolor)
  25.     gimp.set_background(bgcolor)
  26.  
  27.     # Get position of current selection, if there is one
  28.     # Otherwise, put the new text near top-left of image
  29.     non_empty, x1, y1, x2, y2 = pdb.gimp_selection_bounds(img)
  30.  
  31.     if non_empty == True:
  32.         x = x1
  33.         y = y1
  34.     else:
  35.         x = 5
  36.         y = 5
  37.  
  38.     # Create a new text layer (-1 for the layer means create a new layer)
  39.     text_layer = pdb.gimp_text_fontname(
  40.         img, None, x, y, initstr, -1, True, size, PIXELS, font)
  41.  
  42.     # Path from text
  43.     text_vectors = pdb.gimp_vectors_new_from_text_layer(img, text_layer)
  44.  
  45.     pdb.gimp_image_insert_vectors(img, text_vectors, None, 0)
  46.  
  47.     # Make the layer a little bigger
  48.     w = text_layer.width
  49.     h = text_layer.height
  50.    
  51.     text_layer.resize(w + 10, h + 10, 5, 5)
  52.  
  53.     # New layer for the outline
  54.     bg_layer = img.new_layer(munge(img, initstr + ' Background'))
  55.  
  56.     # Select the path created earlier
  57.     pdb.gimp_image_select_item(img, CHANNEL_OP_REPLACE, text_vectors)
  58.  
  59.     # Same as Select -> Grow...
  60.     pdb.gimp_selection_grow(img, outline_radius)
  61.  
  62.     # Fill in the outline
  63.     pdb.gimp_edit_bucket_fill(bg_layer, BG_BUCKET_FILL, NORMAL_MODE,
  64.                               100, 0, False, 0., 0.)
  65.  
  66.     # Apply the blur
  67.     pdb.gimp_selection_all(img)
  68.     if blur_radius > 0:
  69.         pdb.plug_in_gauss_iir(img, bg_layer, blur_radius, True, True)
  70.  
  71.     #Move the outline layer underneath the text layer
  72.     pdb.gimp_image_lower_item(img, img.layers[0])
  73.  
  74.     #Merge outline and text layers
  75.     pdb.gimp_image_merge_down(img, img.layers[0], EXPAND_AS_NECESSARY)
  76.  
  77.     # Show the new image window
  78.     gimp.displays_flush()
  79.  
  80.     # Restore the old foreground color:
  81.     pdb.gimp_context_pop()
  82.  
  83.     # Close the undo group.
  84.     pdb.gimp_undo_push_group_end(img)
  85.  
  86. register(
  87.     "python_fu_glow",
  88.     "Text glow effect",
  89.     "Create glow effect around text",
  90.     "flamethrower",
  91.     "flamethrower",
  92.     "2017",
  93.     "Glow...",
  94.     "*",
  95.     [
  96.         (PF_IMAGE, "image", "Input image", None),
  97.         (PF_DRAWABLE, "drawable", "Input layer", None),
  98.         (PF_STRING, "string", "Text string", 'Hello, world!'),
  99.         (PF_FONT, "font", "Font face", "Arial"),
  100.         (PF_SPINNER, "size", "Font size", 16, (1, 3000, 1)),
  101.         (PF_COLOR, "color", "Text color", (255., 255., 255.)),
  102.         (PF_COLOR, "color", "BG color", (1.0, 0.0, 0.0)),
  103.         (PF_SPINNER, "outline_radius", "Outline radius", 1, (1, 100, 1)),
  104.         (PF_SPINNER, "blur_radius", "Blur radius", 3, (0, 100, 1))
  105.  
  106.     ],
  107.     [],
  108.     do_stuff, menu="<Image>/Filters/Alpha to Logo")
  109.  
  110. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement