[Part I. GIMP基礎功]

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



28.7 電路圖,修改成可「單行」執行的 Python-Fu

修改後的程式碼,與測試的結果圖
  • 啟動 GIMP Python Console
  • 將下面的程式碼,全部選取、複製、再貼到 Python Console 提示符號 >>> 之後,按下 Enter 。
  • 過一會,就會看到結果圖。


inSize = 256
theWidth = inSize
theHeight = inSize
theImage = pdb.gimp_image_new(theWidth, theHeight, RGB)
baseLayer = pdb.gimp_layer_new(theImage, theWidth, theHeight, RGBA_IMAGE, "Background", 100, NORMAL_MODE)
pdb.gimp_image_add_layer(theImage, baseLayer, 0)
# 可以把顯示影像物件 theImage 提前到這裡
pdb.gimp_display_new(theImage)

# 前面的程式碼是為了生成「一個透明的圖層」
# 作為之後生成電路圖案的參考圖層
# 此參考圖層規範了電路圖案的長寬

image = theImage  # 指派影像物件
drawable = baseLayer  # 指派圖層物件

mask_size = 17
seed = 3
remove_bg = FALSE
keep_selection = TRUE
separate_layer = TRUE

type = pdb.gimp_drawable_type_with_alpha(drawable)
image_width = pdb.gimp_image_width(image)
image_height = pdb.gimp_image_height(image)
active_selection = 0
from_selection = 0
selection_bounds = 0
select_offset_x = 0
select_offset_y = 0
select_width = 0
select_height = 0
effect_layer = 0
active_layer = 0

pdb.gimp_context_push()
pdb.gimp_image_undo_group_start(image)

pdb.gimp_layer_add_alpha(drawable)

if pdb.gimp_selection_is_empty(image):
  pdb.gimp_selection_layer_alpha(drawable)
  active_selection = pdb.gimp_selection_save(image)
  from_selection = FALSE
else:
  from_selection = TRUE
  active_selection = pdb.gimp_selection_save(image)


selection_bounds = pdb.gimp_selection_bounds(image)
select_offset_x = selection_bounds[1]
select_offset_y = selection_bounds[2]
select_width = selection_bounds[3] - select_offset_x
select_height = selection_bounds[4] - select_offset_y

floating_sel = 0

if separate_layer:
  effect_layer = pdb.gimp_layer_new(image, select_width, select_height, type, "effect layer", 100, NORMAL_MODE)
  pdb.gimp_image_add_layer(image, effect_layer, -1)
  pdb.gimp_layer_set_offsets(effect_layer, select_offset_x, select_offset_y)
  pdb.gimp_selection_none(image)
  pdb.gimp_edit_clear(effect_layer)
  pdb.gimp_selection_load(active_selection)
  pdb.gimp_edit_copy(drawable)
  floating_sel = pdb.gimp_edit_paste(effect_layer, FALSE)
  pdb.gimp_floating_sel_anchor(floating_sel)
  pdb.gimp_image_set_active_layer(image, effect_layer)
else:
  effect_layer = drawable

active_layer = effect_layer

if remove_bg:
  color = (0, 0, 0)
  pdb.gimp_context_set_foreground(color)
  color = (14, 14, 14)
  pdb.gimp_context_set_foreground(color)


pdb.gimp_selection_load(active_selection)
pdb.plug_in_maze(image, active_layer, 5, 5, TRUE, 0, seed, 57, 1)
pdb.plug_in_oilify(image, active_layer, mask_size, 0)
pdb.plug_in_edge(image, active_layer, 2, 1, 0)

if (type == RGBA_IMAGE):
  pdb.gimp_desaturate(active_layer)

if (remove_bg and separate_layer):
  color = (0, 0, 0)
  pdb.gimp_by_color_select(active_layer, color, 15, 2, TRUE, FALSE, 10, FALSE)
  pdb.gimp_edit_clear(active_layer)

if (keep_selection == FALSE):
  pdb.gimp_selection_none(image)

pdb.gimp_image_remove_channel(image, active_selection)
pdb.gimp_image_set_active_layer(image, drawable)

pdb.gimp_image_undo_group_end(image)

pdb.gimp_displays_flush()

pdb.gimp_context_pop()