[Part I. GIMP基礎功]

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



15.11 「迴圈」的妙處,以「縮放效果」為例

  • 本節展示縮放效果的動畫的程式碼。


縮放效果動畫的製作流程
  • 最初,小島圖案是 512 x 512 。
  • 假設原本共有 16 個大小與內容都相同的圖層。
  • 第 1 個圖層不做裁切 。
  • 剩餘的圖層,每次,上下左右,都裁除 8 個像素。
  • 而後,將裁切後的圖案都縮小為 256 x 256 。
  • 讓所有圖層的左上角都置於 (0, 0) 。
  • 這樣,就會有圖案漸漸放大的效果。


等價的 Python-Fu 程式碼

w = 512
h = 512
s_w = w/2
s_h = h/2
layer_num = 16
amount = 8

pdb.gimp_context_set_gradient("Land 1")
pdb.script_fu_flatland(w, h, 80, 3.0, 4.0, 4.0)
img_array = gimp.image_list()
img = img_array[0] # flat land 這個影像
pdb.gimp_image_flatten(img)

layer1 = pdb.gimp_image_active_drawable(img)
pdb.gimp_drawable_set_name(layer1, "layer 1")

layer_list = []
layer_list.append(layer1)

for i in range(1,layer_num):
  drw = pdb.gimp_layer_copy(layer1, TRUE)
  layer_list.append(drw)
  pdb.gimp_image_add_layer(img, layer_list[i], -1)
  name = "layer " + str(i+1)
  pdb.gimp_drawable_set_name(layer_list[i], name)

pdb.gimp_layer_scale(layer1, s_w, s_h, TRUE)
pdb.gimp_layer_set_offsets(layer1, 0, 0)

for i in range(1,layer_num):
  offx = i * amount
  offy = i * amount
  width = w - 2 * offx
  height = h - 2 * offy
  print offx, offy, width, height
  # 一定要 set active layer ,不然 autocrop layer 有時會失效
  pdb.gimp_image_set_active_layer(img, layer_list[i])
  pdb.gimp_rect_select(img, offx, offx, width, height, 2, FALSE, 0)
  pdb.gimp_selection_invert(img)
  pdb.gimp_edit_cut(layer_list[i])
  pdb.plug_in_autocrop_layer(img, layer_list[i])
  pdb.gimp_layer_scale(layer_list[i], s_w, s_h, TRUE)
  pdb.gimp_layer_set_offsets(layer_list[i], 0, 0)

pdb.gimp_selection_none(img)
pdb.gimp_image_resize_to_layers(img)

  • 把上面的程式碼,貼到 GIMP Python-Fu Console 提示符號 >>> 之後。
  • 而後,可以將檔案存為 GIF 檔。
  • 下圖 GIF 動畫的延遲時間是 1000 ms 。


假設 img_array[0] 代表大象的圖案
  • 修改前面的程式碼
w = 640
h = 640
layer_num = 16
  • 將這兩行註解
#pdb.gimp_context_set_gradient("Land 1")
#pdb.script_fu_flatland(w, h, 80, 3.0, 4.0, 4.0)
  • 開啟大象的圖案,假設 img_array[0] 代表大象的圖案,大象的圖案源自於 Open Clip Art
  • 把前面的程式碼,修改後,全部貼到 GIMP Python-Fu Console 提示符號 >>> 之後。
  • 而後,可以將檔案存為 GIF 檔。
  • 下圖 GIF 動畫的延遲時間是 1000 ms 。