[Part I. GIMP基礎功]

[Part II. 一種效果的誕生]
[Part III. Script-Fu的秘密]

8.11 火燄字的 Python code

等價的 Python-Fu 程式碼
  • 接續前一節的練習操作,改用 Python-Fu 自動化字體的生成流程。
  • 下面的程式碼,分成七大部份:
    1. 首先,要準備好新的影像、新的圖層。
    2. pdb.gimp_text_fontname(...) 寫上字樣 Text
    3. 灰階的外燄
    4. 灰階的內燄
    5. 產生火燄的效果
    6. 漸層字 + 立體字
    7. 調整圖層的結構,與顯示的順序

# Part 1.
# 圖片的尺寸
width = 256
height = 128
# 前景色設為黑色,背景色設為白色
black = (0, 0, 0)
white = (255, 255, 255)
# 前景色設為紅色,背景色設為黃色
red = (255, 0, 0)
yellow =  (255, 255, 0)

img = pdb.gimp_image_new(width, height, RGB)
bg_layer = pdb.gimp_layer_new(img, width, height, RGB_IMAGE, "Black BG with Fire", 100, NORMAL_MODE)
pdb.gimp_image_add_layer(img, bg_layer, 0)
pdb.gimp_context_set_foreground(black)
pdb.gimp_edit_fill(layer, FOREGROUND_FILL)

pdb.gimp_display_new(img)


# Part 2.
string = "Text"
font_size = 72
font = "Arial"
#font = "Sans"
#font = "DejaVu Sans Bold"

text_ext = pdb.gimp_text_get_extents_fontname(string, font_size, PIXELS, font)
text_width = text_ext[0]
text_height= text_ext[1]
# 將 Text 字樣放在適當的地方
text_offx = width/2 - text_width/2
text_offy = height/2 - text_height/2

logo_backup = pdb.gimp_layer_new(img, width, height, RGBA_IMAGE, "Text", 100, NORMAL_MODE)
pdb.gimp_image_add_layer(img, logo_backup, 0)
pdb.gimp_drawable_set_name(logo_backup, "Text Original")
pdb.gimp_context_set_foreground(white)
tmp_layer = pdb.gimp_text_fontname(img, logo_backup, text_width, text_height ,string, 0, TRUE, font_size, PIXELS, font)
pdb.gimp_layer_set_offsets(tmp_layer, text_offx, text_offy)
pdb.gimp_floating_sel_anchor(tmp_layer)


# Part 3.
# 灰階的外燄
fire_outer1 = pdb.gimp_layer_copy(logo_backup, TRUE)
pdb.gimp_image_add_layer(img, fire_outer1, -1)

pdb.gimp_selection_layer_alpha(fire_outer1)

pdb.gimp_selection_grow(img, 2)
pdb.gimp_context_set_foreground(white)
pdb.gimp_edit_fill(fire_outer1, FOREGROUND_FILL)
pdb.gimp_selection_none(img)

fire_outer2 = pdb.gimp_layer_copy(fire_outer1, TRUE)
pdb.gimp_image_add_layer(img, fire_outer2, -1)

blur_radius = 10
pdb.plug_in_gauss_iir(img, fire_outer1, blur_radius, 1, 1)
shift_amount = 50
orientation = 0
pdb.plug_in_shift(img, fire_outer1, shift_amount, orientation)

fire_outer1 = pdb.gimp_image_merge_down(img, fire_outer2, EXPAND_AS_NECESSARY)

pdb.plug_in_shift(img, fire_outer1, shift_amount, orientation)

amplitude = 10
phase = 0
wavelength = 30
#wavelength = 50
pdb.plug_in_waves(img, fire_outer1, amplitude, phase, wavelength, 0, FALSE)

pdb.gimp_rect_select(img, 0, (height*2/3), (width-1), (height-1), 0, TRUE, 4)
pdb.gimp_edit_clear(fire_outer1)
pdb.gimp_selection_none(img)

blur_radius = 10
pdb.plug_in_gauss_iir(img, fire_outer1, blur_radius, 1, 1)

fire_outer2 = pdb.gimp_layer_copy(fire_outer1, TRUE)
pdb.gimp_image_add_layer(img, fire_outer2, -1)
fire_outer = pdb.gimp_image_merge_down(img, fire_outer2, EXPAND_AS_NECESSARY)
pdb.gimp_drawable_set_name(fire_outer, "Fire Outer Layer")

pdb.gimp_layer_set_visible(logo_backup, FALSE)


# Part 4.
# 灰階的內燄
fire_inner = pdb.gimp_layer_copy(logo_backup, TRUE)
pdb.gimp_image_add_layer(img, fire_inner, -1)
pdb.gimp_drawable_set_name(fire_inner, "Fire Inner Layer")

pdb.gimp_selection_layer_alpha(fire_inner)

pdb.gimp_selection_grow(img, 2)

pdb.gimp_context_set_foreground(white)
pdb.gimp_edit_fill(fire_inner, FOREGROUND_FILL)

pdb.gimp_selection_none(img)

blur_radius = 10
pdb.plug_in_gauss_iir(img, fire_inner, blur_radius, 1, 1)


# Part 5.
# 合併外燄、內燄、黑色背景
# 漸層換色,產生火燄的效果
fire_layer = pdb.gimp_image_merge_down(img, fire_inner, EXPAND_AS_NECESSARY)
bg_layer = pdb.gimp_image_merge_down(img, fire_layer, EXPAND_AS_NECESSARY)

pdb.gimp_drawable_set_name(bg_layer, "Black BG with Fire")

gradient = "German flag smooth"
pdb.gimp_context_set_gradient(gradient)
pdb.plug_in_gradmap(img, bg_layer)


# Part 6.
# 漸層效果 + 立體效果
pdb.gimp_layer_set_visible(logo_backup, TRUE)

logo_layer = pdb.gimp_layer_copy(logo_backup, TRUE)
pdb.gimp_image_add_layer(img, logo_layer, -1)
pdb.gimp_drawable_set_name(logo_layer, "Gradient Effect Logo")

pdb.gimp_layer_set_lock_alpha(logo_layer, TRUE)
pdb.gimp_context_set_foreground(yellow)
pdb.gimp_context_set_background(red)

offset =0 ;repeat = REPEAT_NONE;reverse = FALSE;
supersample = FALSE ; max_depth = 0 ; threshold = 0;
dither = TRUE ;

x1 = 0; y1 = height/2; x2 = width; y2 = height/2 # 相當於由左至右拉一條直線

pdb.gimp_blend(logo_layer,  FG_BG_RGB_MODE, NORMAL_MODE, GRADIENT_LINEAR, 100, offset, repeat, reverse, supersample, max_depth, threshold, dither, x1, y1, x2, y2)
#
pdb.plug_in_bump_map(img, logo_layer, logo_layer, 135.0, 35, 3, 0, 0, 0, 0, TRUE, FALSE, 0)


# Part 7.
# 讓漸層效果「Text」文字圖案顯示在最上方的圖層
# 讓「火燄效果 」烘托「Text」文字圖案
pdb.gimp_image_raise_layer_to_top(img, logo_layer)

pdb.gimp_layer_set_visible(logo_backup, FALSE)

  • 啟動 GIMP Python Console
  • 將前面的程式碼,依次序,全部選取、複製、再貼到 Python Console 提示符號 >>> 之後,按下 Enter 。
  • 過一會,就會看到火燄字,與其圖層結構,如下圖所示。

font = "DejaVu Sans Bold"
wavelength = 10


font = "DejaVu Sans Bold"
wavelength = 30


font = "DejaVu Sans Bold"
wavelength = 50


font = "Arial"
wavelength = 30


font = "Arial"
wavelength = 30


font = "Arial"
wavelength = 50