'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. This seems quite simple but I am stuck with pdb calls, I may not be calling procedures correctly so I need your help.
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:
Error when calling « gimp-procedural-db-proc-info » : Procedure« vectors-export-to-file » not found
When I search in Gimp PDB, I can find"gimp-vectors-export-to-file" so what is the problem ? How should I call this procedure ?
Solution 1:[1]
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).
But what is really causing your error is the procedure is actually called
gimp_vectors_export_to_file
- and not 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. The "Vectors" object for saving individual Vectors is obtained by calling other PDB functions. For saving all vectors , instead of the "0" listed in the function description, pass 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.
To get a reference to your image in the interactive console, type:
img = gimp.image_list()[0]
Solution 2:[2]
I've adapted this question into a gimp python plugin that exposes this functionality as the svg export format.
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
Copy this source into the gimp plugin directory (and make it executable).
On my system that is ~/.gimp-2.8/plug-ins/file-svg-export.py
, on windows its probably somewhere in %appdata%
.
#!/usr/bin/env python
# GIMP Plug-in for Simple SVG Exports
# Copyright (C) 2016 by Dylan Grafmyre <[email protected]>
#
# 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
It will export all paths to a single svg file.
Solution 3:[3]
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.
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".
Tested with GIMP version 2.8 and 2.10.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | ThorSummoner |
Solution 3 | szci2 |