[Part I. GIMP基礎功]

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



30.7 老照片,修改成可「單行」執行的 Python-Fu

使用紅底黑框的圖案,來測試老照片的效果
  • 原本的old-photo.scm需要參考到既存的影像物件、圖層物件,才能正確運行 。
  • 添加「建立」的程式碼,這樣「老照片效果的程式」才能正確運行 。
  • 下圖是程式碼執行後的圖案,紅底黑框附帶兩個青色小方框。


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

color = (255, 0, 0)
pdb.gimp_context_set_background(color)
pdb.gimp_selection_all(img)
pdb.gimp_edit_fill(layer_one, BACKGROUND_FILL) #塗滿紅色

# 重設為預設的筆刷 Circle (11)
pdb.gimp_context_set_brush("Circle (11)")

color = (0, 0, 0)
pdb.gimp_context_set_foreground(color)
pdb.gimp_selection_shrink(img, 10)
pdb.gimp_edit_stroke(layer_one) #描繪出一個黑色外框

pdb.gimp_selection_none(img)

color = (0, 255, 0)
pdb.gimp_context_set_foreground(color)
pdb.gimp_context_set_brush("Circle (03)")
pdb.gimp_rect_select(img, 50, 50, 50, 50, 0, TRUE, 4)
pdb.gimp_edit_stroke(layer_one) #描繪出一個青色方框

pdb.gimp_selection_none(img)

pdb.gimp_rect_select(img, 150, 150, 50, 50, 0, TRUE, 4)
pdb.gimp_edit_stroke(layer_one)
pdb.gimp_selection_none(img) #描繪出一個青色方框


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


# 要先準備好「測試用的影像與圖層」
inImage = img # 指派影像物件
inLayer = layer_one # 指派圖層物件

inDefocus = TRUE
inBorderSize = 20
inSepia = TRUE
inMottle = FALSE
inCopy = TRUE

theImage = 0
theLayer = 0
theWidth = 0
theHeight = 0

pdb.gimp_image_undo_group_start(inImage)
pdb.gimp_selection_all(inImage)

if (inCopy):
  theImage = pdb.gimp_image_duplicate(inImage)

theLayer = pdb.gimp_image_flatten(theImage)

if (inDefocus):
  pdb.plug_in_gauss_rle(theImage, theLayer, 1.5, TRUE, TRUE)

if (inBorderSize > 0):
  color = (255, 255, 255)
  pdb.script_fu_fuzzy_border(theImage, theLayer, color, inBorderSize, TRUE, 8, FALSE, 100, FALSE, TRUE)

theLayer = pdb.gimp_image_flatten(theImage)

if (inSepia):
  pdb.gimp_desaturate(theLayer)
  pdb.gimp_brightness_contrast(theLayer, -20, -40)
  pdb.gimp_color_balance(theLayer, 0, TRUE, 30, 0, -30)

theWidth = pdb.gimp_image_width(theImage)
theHeight = pdb.gimp_image_height(theImage)
mLayer = pdb.gimp_layer_new(theImage, theWidth, theHeight, RGBA_IMAGE, "Mottle", 100, DARKEN_ONLY_MODE)

if (inMottle):
  pdb.gimp_image_add_layer(theImage, mLayer, 0)
  pdb.gimp_selection_all(theImage)
  pdb.gimp_edit_clear(mLayer)
  pdb.gimp_selection_none(theImage)
  pdb.plug_in_noisify(theImage, mLayer, TRUE, 0, 0, 0, 0.5)
  pdb.plug_in_gauss_rle(theImage, mLayer, 5, TRUE, TRUE)
  theLayer = pdb.gimp_image_flatten(theImage)

if (inCopy):
  pdb.gimp_image_clean_all(theImage)
  pdb.gimp_display_new(theImage)

pdb.gimp_selection_none(inImage)
pdb.gimp_image_undo_group_end(inImage)
pdb.gimp_displays_flush((theImage)