[Part I. GIMP基礎功]

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

28.8 電路圖,修改過程中,須注意的 Python 語法特點

計算出選取區域的長寬
Scheme code
(set! selection-bounds (gimp-selection-bounds image))
(set! select-offset-x (cadr selection-bounds))
(set! select-offset-y (caddr selection-bounds))
(set! select-width (- (cadr (cddr selection-bounds)) select-offset-x))
(set! select-height (- (caddr (cddr selection-bounds)) select-offset-y))

Python code
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-else 條件控制的敘述
Scheme code 要注意括號的對齊

(if (= (car (gimp-selection-is-empty image)) TRUE)
(begin
(gimp-selection-layer-alpha drawable)
(set! active-selection (car (gimp-selection-save image)))
(set! from-selection FALSE))
(begin
(set! from-selection TRUE) ; 條件不成立,會執行這兩行
(set! active-selection (car (gimp-selection-save image)))
) ; 此為 begin 的右括號
) ; 此為 if 的右括號

Python code 要注意縮排

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)


RGB 顏色的指定
Scheme code
(gimp-context-set-foreground '(0 0 0))

Python code 拆成兩行來達到設定前景色的功能
color = (0, 0, 0)
pdb.gimp_context_set_foreground(color)