简体   繁体   English

使用 Gimp Python 插件导出所有 SVG 路径

[英]Export all SVG paths with Gimp Python plugin

I want to write a Gimp-Python plugin in order to export all SVG paths from the current image.我想编写一个 Gimp-Python 插件,以便从当前图像中导出所有 SVG 路径。 This seems quite simple but I am stuck with pdb calls, I may not be calling procedures correctly so I need your help.这看起来很简单,但我被 pdb 调用困住了,我可能没有正确调用过程,所以我需要你的帮助。

Here is my code:这是我的代码:

#!/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()

After opening Gimp and loading an image, when I click on my plugin i receive this error:打开 Gimp 并加载图像后,当我点击我的插件时,我收到此错误:

Error when calling « gimp-procedural-db-proc-info » : Procedure« vectors-export-to-file » not found调用 « gimp-procedural-db-proc-info » 时出错:找不到程序 « vectors-export-to-file »

When I search in Gimp PDB, I can find"gimp-vectors-export-to-file" so what is the problem?当我在 Gimp PDB 中搜索时,我可以找到“gimp-vectors-export-to-file”,这是什么问题? How should I call this procedure?我应该如何调用这个过程?

You are almost there - the "pdb" module is just exposed at module level with the from gimpfu import * so, no need to do gimp.pdb.<procname> - just pdb.<procname> (it would work, though). 你几乎就在那里 - “pdb”模块只是在模块级别暴露了from gimpfu import *所以,不需要做gimp.pdb.<procname> - 只是pdb.<procname> (虽然它会起作用)。

But what is really causing your error is the procedure is actually called gimp_vectors_export_to_file - and not vectors_export_to_file - 但真正导致错误的是该过程实际上称为gimp_vectors_export_to_file - 而不是vectors_export_to_file -

You should be calling it as: 你应该把它称为:

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

Also note that for most PDB calls requiring integer identifiers of image items, you should pass an apropriate Python object representing that item instead. 另请注意,对于需要图像项的整数标识符的大多数PDB调用,您应该传递表示该项的适当Python对象。 The "Vectors" object for saving individual Vectors is obtained by calling other PDB functions. 用于保存单个向量的“向量”对象是通过调用其他PDB函数获得的。 For saving all vectors , instead of the "0" listed in the function description, pass None . 要保存所有向量,而不是函数描述中列出的“0”,请传递None

NB: there is a Python interactive console you can access using filters->python->console - it is much easier to code for these plug-ins and scripts if you test your pdb calls and image manipulations interactively first. 注意:您可以使用filters->python->console访问Python交互式控制台 - 如果您首先以交互方式测试pdb调用和图像操作,则可以更轻松地为这些插件和脚本编写代码。

To get a reference to your image in the interactive console, type: 要在交互式控制台中获取对图像的引用,请键入:

img = gimp.image_list()[0]

I've adapted this question into a gimp python plugin that exposes this functionality as the svg export format. 我已经将这个问题改编成了一个gimp python插件,它将此功能公开为svg导出格式。

I have made it into a gist, pull requests welcome, the gist may be more featureful than this answer. 我已经把它变成了一个要点,拉请求欢迎,要点可能比这个答案更有特色。

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

Copy this source into the gimp plugin directory (and make it executable). 将此源复制到gimp插件目录(并使其可执行)。

On my system that is ~/.gimp-2.8/plug-ins/file-svg-export.py , on windows its probably somewhere in %appdata% . 在我的系统~/.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()

To use simply File -> Export As, your file.svg 要简单地使用File - > Export As, your file.svg

It will export all paths to a single svg file. 它会将所有路径导出到单个svg文件。

This doesn't technically solve the problem using a Python plugin, but for those who have ended up here because they wanted to export all paths at once, GIMP allows you to export all paths without any plugins.这在技术上并不能解决使用 Python 插件的问题,但是对于那些因为想一次导出所有路径而走到这里的人来说,GIMP 允许您在没有任何插件的情况下导出所有路径。

To export all paths as opposed to a single path, right click any path, select "Export path..." and change the option in the window from "Export the active path" to "Export all paths from this image".要导出所有路径而不是单个路径,请右键单击任何路径,select“导出路径...”并将 window 中的选项从“导出活动路径”更改为“从此图像导出所有路径”。

“将路径导出到 SVG”对话框

Tested with GIMP version 2.8 and 2.10.使用 GIMP 版本 2.8 和 2.10 进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM