1. 程式人生 > 實用技巧 >GIMP 一鍵均勻新增多條參考線

GIMP 一鍵均勻新增多條參考線

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

from gimpfu import *

# orientation: ORIENTATION_HORIZONTAL(0), ORIENTATION_VERTICAL(1)
# diff: 參考線之間的間隔
def add_multi_guides(orientation, diff):
    imgs = gimp.image_list()
    if len(imgs) == 0:
        return
    img = imgs[0]

    w, h = img.width, img.height
    endposition = None
    add_guide = None

    if orientation == ORIENTATION_HORIZONTAL:
        assert diff < h, 'diff too big'
        endposition = h
        add_guide = pdb.gimp_image_add_hguide
    elif orientation == ORIENTATION_VERTICAL:
        assert diff < w, 'diff too big'
        endposition = w
        add_guide = pdb.gimp_image_add_vguide
    else:
        raise ValueError(('orientation not valid: {0}').format(orientation))

    # 清空原來的參考線
    guide = pdb.gimp_image_find_next_guide(img, 0)
    while guide != 0:
        if orientation == pdb.gimp_image_get_guide_orientation(img, guide):
            pdb.gimp_image_delete_guide(img, guide)
            guide = 0
        guide = pdb.gimp_image_find_next_guide(img, guide)

    position = diff
    while position < endposition:
        add_guide(img, position)
        position = position + diff


register(
    "add_multi_guides",
    # table snippet means a small piece of HTML code here
    "Add fucking guides",
    "long description",
    "hangj",
    "hangj",
    "2020",
    "Add Multi Guides...",
    "",
    [
        (PF_OPTION, "orientation", "orientation", 0, ("HORIZONTAL", "VERTICAL")),
        (PF_INT, "diff", "pixcels between guides", 1000)
    ],
    [],
    add_multi_guides,
    menu="<Image>/Image/Guides"
    )

main()

把指令碼儲存,放到 plug-ins 目錄下,然後chmod +x filename,重啟 GIMP