Rsvg question

I cut and paste below code from the web. I am trying to convert a svg file to png.
The problem is
the output file has only a size of 137 bytes.
The Mac finder displays an empty content.
I expect the file size to be about a few k bytes.

Can someone point me to the problems ?

thanks
HP


using Rsvg
using Cairo

filename_in = “/path_to/wq.svg”
filename_out= “/path_to/wQ.png”

new_height = 100

r = Rsvg.handle_new_from_file(filename_in)
d = Rsvg.handle_get_dimensions®

println(d.width)
println(d.height)

scalingfactor = new_height / d.height

cs = Cairo.CairoImageSurface(round(Int,d.width * scalingfactor), new_height, Cairo.FORMAT_ARGB32)

c = Cairo.CairoContext(cs)
Cairo.scale(c, scalingfactor, scalingfactor)
Rsvg.handle_render_cairo(c,r)

Cairo.write_to_png(cs,filename_out) # 20210307 not working ?


Could be that you need to call Cairo.finish or something like that first

I tried to insert
Cairo.finish(cs)
right before Cairo.write_to_png()
or right after it.

Both still produced an empty file (137 bytes).
[ I also move the finish() in a few other places and no luck… ]

Thanks
HP

Sounds like the file wasn’t loaded? If you want an alternative workflow, you could do:

using Luxor
s = readsvg("/tmp/test1.svg")
w, h = s.width, s.height
@png begin
    placeimage(s, centered=true)
end w h "/tmp/output.png"

which calls the same Cairo.jl and RSvg.jl functions.

I assume something is not well-formed-svg in your input. Rsvg.jl has some test data in the /data directory. I just checked with star.svg and it works.

1 Like

Yes, indeed.
I did not think of corrupted source (svg) files!!

It turns out that the svg files somehow got corrupted last night when I tried
various snippets…

Thanks a lot.
HP

1 Like