[Part I. GIMP基礎功]

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

29.7 熔岩圖,修改成可「單行」執行的 Python-Fu

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


width = 256
height = 256
img = pdb.gimp_image_new(width, height, RGB)
layer_one = pdb.gimp_layer_new(img, width, height, RGBA_IMAGE, "layer 1", 100, NORMAL_MODE)
pdb.gimp_image_add_layer(img, layer_one, 0)
pdb.gimp_display_new(img)

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

image = img # 指派影像物件
drawable = layer_one # 指派圖層物件

# 可以生成一個矩形選取區域。
# 測試有選取區域時的效果。
# pdb.gimp_rect_select(img, 25, 25, 150, 150, 0, TRUE, 4)

seed = 10
tile_size = 10
mask_size = 7
gradient = "German flag smooth"
keep_selection = TRUE
separate_layer = TRUE
current_grad = FALSE

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
selection_bounds = 0
select_offset_x = 0
select_offset_y = 0
select_width = 0
select_height = 0
lava_layer = 0
active_layer = 0

pdb.gimp_context_push()
pdb.gimp_image_undo_group_start(image)

if pdb.gimp_drawable_has_alpha(drawable) == FALSE:
  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)
pdb.gimp_image_set_active_layer(image, drawable)

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

if separate_layer:
  lava_layer = pdb.gimp_layer_new(image, select_width, select_height, type, "Lava Layer", 100, NORMAL_MODE)
  pdb.gimp_image_add_layer(image, lava_layer, -1)
  pdb.gimp_layer_set_offsets(lava_layer, select_offset_x, select_offset_y)
  pdb.gimp_selection_none(image)
  pdb.gimp_edit_clear(lava_layer)
  pdb.gimp_selection_load(active_selection)
  pdb.gimp_image_set_active_layer(image, lava_layer)

active_layer = pdb.gimp_image_get_active_layer(image)

if current_grad == FALSE:
  pdb.gimp_context_set_gradient(gradient)

pdb.plug_in_solid_noise(image, active_layer, FALSE, TRUE, seed, 2, 2, 2)
pdb.plug_in_cubism(image, active_layer, tile_size, 2.5, 0)
pdb.plug_in_oilify(image, active_layer, mask_size, 0)
pdb.plug_in_edge(image, active_layer, 2, 0, 0)
pdb.plug_in_gauss_rle(image, active_layer, 2, TRUE, TRUE)
pdb.plug_in_gradmap(image, active_layer)

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

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