You are not logged in.

#1 2008-12-30 15:57:43

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

How to Render a PNG from an SVG via Command-line

I have inkscape, gimp, and imagemagick installed on my arch64 system and I need to render a PNG from an SVG via command-line. I'd like to be able to specify...

-location of SVG
-output location of PNG
-dpi
-anti-aliasing

Does anyone know a way to do this?

Offline

#2 2008-12-30 17:07:28

dwi
Member
Registered: 2008-01-27
Posts: 27

Re: How to Render a PNG from an SVG via Command-line

It appears that Inkscape can do most, if not all of this, currently.

See this link.

Offline

#3 2008-12-30 17:19:48

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: How to Render a PNG from an SVG via Command-line

Search for convert from imagemagick.

Offline

#4 2008-12-30 17:33:32

arch0r
Member
From: From the Chron-o-John
Registered: 2008-05-13
Posts: 597

Re: How to Render a PNG from an SVG via Command-line

http://inkscape-forum.andreas-s.net/topic/133909

Last edited by arch0r (2008-12-30 17:34:48)

Offline

#5 2008-12-30 17:51:47

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: How to Render a PNG from an SVG via Command-line

I have tried svg2png once but it messed up one of my svgs (it rendered fine in firefox, so I'm not sure what was wrong)

Offline

#6 2008-12-30 18:25:23

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to Render a PNG from an SVG via Command-line

Hrm... I am trying imagemagick but am having a weird loss of colour:

SVG: http://norpass.com/angle.svg

Command: "convert angle.svg angle.png"

PNG: http://norpass.com/angle.png

Any ideas?

Offline

#7 2008-12-30 18:34:44

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: How to Render a PNG from an SVG via Command-line

tony5429 wrote:

Hrm... I am trying imagemagick but am having a weird loss of colour:

SVG: http://norpass.com/angle.svg

Command: "convert angle.svg angle.png"

PNG: http://norpass.com/angle.png

Any ideas?

I'm seeing it too, even with 100% png quality. Do you intend to do a lot of these conversions? If so, do you have GIMP installed and are you interested in GIMP Python scripts as a solution?

Offline

#8 2008-12-30 18:38:11

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to Render a PNG from an SVG via Command-line

Yes; if gimp can do it via command-line and produce an image like what firefox 3 renders of the svg, that would be great.

Offline

#9 2008-12-30 19:08:21

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to Render a PNG from an SVG via Command-line

And with inkscape (using "inkscape -f angle.svg -e angle.png") the red line doesn't show up at all... How can I do it with GIMP, Skottish? thanks.

Offline

#10 2008-12-30 19:10:45

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to Render a PNG from an SVG via Command-line

I tried importing the angle.svg into gimp and found that it also ignores the red line... Maybe I will just live with imagemagick's output.

Offline

#11 2008-12-30 19:35:47

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: How to Render a PNG from an SVG via Command-line

Here's a start anyway. It's been a while since I've done anything like this. Currently it reads all image files of a certain pattern out of the current directory (.svg, .jpg, .tif, whatever GIMP can read), converts them to the default png values, and outputs them to some directory that you specify. I'll keep trying to refine it:

#! /usr/bin/env python
from gimpfu import *
import glob
import os

def svgtopng(file_pattern, output_directory):

    file_list=glob.glob(file_pattern)

    for file_name in file_list:

    image = pdb.gimp_file_load(file_name, file_name)
    drawable = pdb.gimp_image_get_active_layer(image)

    file_name = output_directory + "/" + os.path.splitext(file_name)[0] + ".png"

    pdb.file_png_save_defaults(image, drawable, file_name, file_name)

    pdb.gimp_image_delete(image)


register(
  "svgtopng", "", "", "", "", "",
  "<Toolbox>/Xtns/Languages/Python-Fu/svgtopng", "",
  [
  (PF_STRING, "file_name", "file_name", "file_name"),
  (PF_STRING, "output_directory", "output_directory", "output_directory")
  ],
  [],
  svgtopng
  )

main()

You call it from the directory of your svg files by this. I'll just call the output directory /home/tony5429/output:

gimp -inbdf '(python-fu-svgtopng RUN-NONINTERACTIVE "*.svg" "/home/tony5429/output")''(gimp-quit 1)'

There is absolutely no error checking here. Also, you need to put this file somewhere where GIMP can find it. Call it svgtopng.py, put in the plugins folder ~/.gimp-2.6/plug-ins, and make sure that it's executable.

Offline

#12 2008-12-30 19:55:46

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to Render a PNG from an SVG via Command-line

Thanks for your help. However, I received the following error when trying your script:

[karam@nikolai ~]$ cd svgtest/
[karam@nikolai svgtest]$ gimp -inbdf '(python-fu-svgtopng RUN-NONINTERACTIVE "*.svg" "/home/karam/output")''(gimp-quit 1)'
/usr/lib/gimp/2.0/plug-ins/help-browser: error while loading shared libraries: libwebkit-1.0.so.1: cannot open shared object file: No such file or directory

(gimp:5663): LibGimpBase-WARNING **: gimp: gimp_wire_read(): error
  File "/home/karam/.gimp-2.6/plug-ins/svgtopng.py", line 12
    image = pdb.gimp_file_load(file_name, file_name)
        ^
IndentationError: expected an indented block

(gimp:5663): LibGimpBase-WARNING **: gimp: gimp_wire_read(): error
batch command experienced an execution error

Offline

#13 2008-12-30 19:58:12

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: How to Render a PNG from an SVG via Command-line

Sorry about. It looks like it didn't copy and paste well. I'll edit the above post and it should be fine.

--EDIT--

It's the forum. You need to indent all of the lines starting with image and ending in pdb.gimp_image_delete. I'm using four spaces as indentation.

Offline

#14 2008-12-30 20:45:47

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to Render a PNG from an SVG via Command-line

Hm...Now I'm getting this error:

[karam@nikolai svgtest]$ gimp -inbdf '(python-fu-svgtopng RUN-NONINTERACTIVE "*.svg" "/home/karam/output")''(gimp-quit 1)'
/usr/lib/gimp/2.0/plug-ins/help-browser: error while loading shared libraries: libwebkit-1.0.so.1: cannot open shared object file: No such file or directory

(gimp:5756): LibGimpBase-WARNING **: gimp: gimp_wire_read(): error

[karam@nikolai svgtest]$

Offline

#15 2008-12-30 20:56:19

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: How to Render a PNG from an SVG via Command-line

tony5429 wrote:

Hm...Now I'm getting this error:

[karam@nikolai svgtest]$ gimp -inbdf '(python-fu-svgtopng RUN-NONINTERACTIVE "*.svg" "/home/karam/output")''(gimp-quit 1)'
/usr/lib/gimp/2.0/plug-ins/help-browser: error while loading shared libraries: libwebkit-1.0.so.1: cannot open shared object file: No such file or directory

(gimp:5756): LibGimpBase-WARNING **: gimp: gimp_wire_read(): error

[karam@nikolai svgtest]$

Your getting an error about not having libwebkit installed. Can you start GIMP normally?

Also, the script won't output any information unless it fails. If you want some sort of confirmation, put a line like this after pdb.gimp_image_delete(image). Use the same indentation:

print file_name + " saved."

Offline

#16 2008-12-30 21:04:05

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to Render a PNG from an SVG via Command-line

You're right, I get that error every time I load gimp. The script did create angle.png in the output directory - however, the red line of the graph is missing completely. Are you able to use the angle.svg (http://norpass.com/angle.svg) to produce an angle.png with the red line?

Offline

#17 2008-12-30 21:17:37

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: How to Render a PNG from an SVG via Command-line

Yes. The test file that I used is yours (just retested). The output png that I get is a transparent image with the graph intact. The red lines are there and it doesn't suffer from the gray artifacts that convert was producing.

Offline

#18 2009-01-01 16:29:18

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to Render a PNG from an SVG via Command-line

Weird...Maybe I am using an outdated version of Gimp or something. When I get internet again on that machine (I've only got wireless here and that machine only has ethernet), I'll do a system upgrade and try your script again. Thanks for all your help!

Offline

Board footer

Powered by FluxBB