簡體   English   中英

使用 Gimp Python 插件導出所有 SVG 路徑

[英]Export all SVG paths with Gimp Python plugin

我想編寫一個 Gimp-Python 插件,以便從當前圖像中導出所有 SVG 路徑。 這看起來很簡單,但我被 pdb 調用困住了,我可能沒有正確調用過程,所以我需要你的幫助。

這是我的代碼:

#!/usr/bin/env python

from gimpfu import *

def exportToSvg(img, layer) :
    gimp.pdb.vectors_export_to_file(img,"C:/Users/Public/Documents/output.svg",0)

register(
    "ExportToSvg",    
    "Export all SVG paths",   
    "Export all SVG paths from current image",
    "My Name", 
    "My company", 
    "2015",
    "<Image>/MyScripts/Export to svg", 
    "*", 
    [], 
    [],
    exportToSvg)

main()

打開 Gimp 並加載圖像后,當我點擊我的插件時,我收到此錯誤:

調用 « gimp-procedural-db-proc-info » 時出錯:找不到程序 « vectors-export-to-file »

當我在 Gimp PDB 中搜索時,我可以找到“gimp-vectors-export-to-file”,這是什么問題? 我應該如何調用這個過程?

你幾乎就在那里 - “pdb”模塊只是在模塊級別暴露了from gimpfu import *所以,不需要做gimp.pdb.<procname> - 只是pdb.<procname> (雖然它會起作用)。

但真正導致錯誤的是該過程實際上稱為gimp_vectors_export_to_file - 而不是vectors_export_to_file -

你應該把它稱為:

pdb.gimp_vectors_export_to_file(img,"C:/Users/Public/Documents/output.svg",None)

另請注意,對於需要圖像項的整數標識符的大多數PDB調用,您應該傳遞表示該項的適當Python對象。 用於保存單個向量的“向量”對象是通過調用其他PDB函數獲得的。 要保存所有向量,而不是函數描述中列出的“0”,請傳遞None

注意:您可以使用filters->python->console訪問Python交互式控制台 - 如果您首先以交互方式測試pdb調用和圖像操作,則可以更輕松地為這些插件和腳本編寫代碼。

要在交互式控制台中獲取對圖像的引用,請鍵入:

img = gimp.image_list()[0]

我已經將這個問題改編成了一個gimp python插件,它將此功能公開為svg導出格式。

我已經把它變成了一個要點,拉請求歡迎,要點可能比這個答案更有特色。

https://gist.github.com/thorsummoner/3ad6f806f1c08246f240222a3c0a5c47

將此源復制到gimp插件目錄(並使其可執行)。

在我的系統~/.gimp-2.8/plug-ins/file-svg-export.py ,在windows上它可能在%appdata%某個地方。

#!/usr/bin/env python

# GIMP Plug-in for Simple SVG Exports

# Copyright (C) 2016 by Dylan Grafmyre <thorsummoner@live.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# based on an openraster plugin by 
# https://git.gnome.org/browse/gimp/tree/plug-ins/pygimp/plug-ins/file-openraster.py?h=GIMP_2_8_16

import gimpfu

def register_save_handlers():
    gimpfu.gimp.register_save_handler('file-svg-save', 'svg', '')

def save_svg(img, drawable, filename, raw_filename):
    gimpfu.gimp.pdb.gimp_vectors_export_to_file(img, filename, None)

gimpfu.register(
    'file-svg-save', #name
    'save an SVG (.svg) file', #description
    'save an SVG (.svg) file',
    'Dylan Grafmyre', #author
    'Dylan Grafmyre', #copyright
    '2016', #year
    'SVG',
    '*',
    [   #input args. Format (type, name, description, default [, extra])
        (gimpfu.PF_IMAGE, "image", "Input image", None),
        (gimpfu.PF_DRAWABLE, "drawable", "Input drawable", None),
        (gimpfu.PF_STRING, "filename", "The name of the file", None),
        (gimpfu.PF_STRING, "raw-filename", "The name of the file", None),
    ],
    [], #results. Format (type, name, description)
    save_svg, #callback
    on_query = register_save_handlers,
    menu = '<Save>'
)

gimpfu.main()

要簡單地使用File - > Export As, your file.svg

它會將所有路徑導出到單個svg文件。

這在技術上並不能解決使用 Python 插件的問題,但是對於那些因為想一次導出所有路徑而走到這里的人來說,GIMP 允許您在沒有任何插件的情況下導出所有路徑。

要導出所有路徑而不是單個路徑,請右鍵單擊任何路徑,select“導出路徑...”並將 window 中的選項從“導出活動路徑”更改為“從此圖像導出所有路徑”。

“將路徑導出到 SVG”對話框

使用 GIMP 版本 2.8 和 2.10 進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM