[Part I. GIMP基礎功]

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



27.7 迷彩圖,修改成可「單行」執行的 Python-Fu

  • 啟動 GIMP Python Console
  • 將下面的程式碼,全部選取、複製、再貼到 Python Console 提示符號 >>> 之後,按下 Enter 。
  • 過一會,就會看到迷彩圖。
inSize = 256
inGrain = 7
inColor1 = (33, 100, 58)
inColor2 = (170, 170, 60)
inColor3 = (150, 115, 100)
inSmooth = FALSE
inFlatten = TRUE

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)
thickLayer = 0
thinLayer = 0
theBlur = 0

#可以把顯示影像物件 theImage 提前到這裡
pdb.gimp_display_new(theImage)

pdb.gimp_context_push()

pdb.gimp_image_add_layer(theImage, baseLayer, 0)

thickLayer = pdb.gimp_layer_new(theImage, theWidth, theHeight, RGBA_IMAGE, "Camo Thick Layer", 100, NORMAL_MODE)
pdb.gimp_image_add_layer(theImage, thickLayer, 0)

thinLayer = pdb.gimp_layer_new(theImage, theWidth, theHeight, RGBA_IMAGE, "Camo Thin Layer", 100, NORMAL_MODE)
pdb.gimp_image_add_layer(theImage, thinLayer, 0)

pdb.gimp_selection_all(theImage)
pdb.gimp_context_set_background(inColor1)
pdb.gimp_drawable_fill(baseLayer, BACKGROUND_FILL)

rand_num1 = -17265
rand_num2 = 22498
pdb.plug_in_solid_noise(theImage, thickLayer, 1, 0, rand_num1, 1, inGrain, inGrain)
pdb.plug_in_solid_noise(theImage, thinLayer, 1, 0, rand_num2, 1, inGrain, inGrain)
pdb.gimp_threshold(thickLayer, 127, 255)
pdb.gimp_threshold(thinLayer, 145, 255)

theBlur = (16 - inGrain)

pdb.gimp_context_set_background(inColor2)
color_black = (0, 0, 0)

pdb.gimp_by_color_select(thickLayer, color_black, 127, CHANNEL_OP_REPLACE, TRUE, FALSE, 0, FALSE)
pdb.gimp_edit_clear(thickLayer)
pdb.gimp_selection_invert(theImage)
pdb.gimp_edit_fill(thickLayer, BACKGROUND_FILL)
pdb.gimp_selection_none(theImage)

if (inSmooth):
  pdb.script_fu_tile_blur(theImage, thickLayer, theBlur, TRUE, TRUE, FALSE)

pdb.gimp_context_set_background(inColor3)
pdb.gimp_by_color_select(thinLayer, color_black, 127, CHANNEL_OP_REPLACE, TRUE, FALSE, 0, FALSE)
pdb.gimp_edit_clear(thinLayer)
pdb.gimp_selection_invert(theImage)
pdb.gimp_edit_fill(thinLayer, BACKGROUND_FILL)
pdb.gimp_selection_none(theImage)

if (inSmooth):
  pdb.script_fu_tile_blur(theImage, thinLayer, theBlur / 2, TRUE, TRUE, FALSE)

if (inFlatten):
  pdb.gimp_image_flatten(theImage)

#註解
#pdb.gimp_display_new(theImage)
pdb.gimp_context_pop()