# SVG to PNG

**URL:** https://discourse.julialang.org/t/svg-to-png/18643
**Category:** New to Julia
**Tags:** visualization
**Created:** [December 13, 2018, 10:52pm UTC](https://discourse.julialang.org/t/svg-to-png/18643 "2018-12-13T22:52:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wsrinin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsrinin/32/7676_2.png) [@wsrinin](https://discourse.julialang.org/u/wsrinin)
#### Post date: [December 13, 2018, 10:52pm UTC](https://discourse.julialang.org/t/svg-to-png/18643/1 "2018-12-13T22:52:36Z")

</div>

Hello,

Is there a package that allows me to convert SVG file to PNG file?

Best,

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [December 13, 2018, 11:18pm UTC](https://discourse.julialang.org/t/svg-to-png/18643/2 "2018-12-13T23:18:17Z")

</div>

> **[GitHub - lobingera/Rsvg.jl: adaptation of the rsvg lib](https://github.com/lobingera/Rsvg.jl)**
>
> adaptation of the rsvg lib. Contribute to lobingera/Rsvg.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)
#### Post date: [January 20, 2020, 4:27pm UTC](https://discourse.julialang.org/t/svg-to-png/18643/3 "2020-01-20T16:27:49Z")

</div>

I recently needed a version with fixed pixel size, which you can find [here](https://discourse.julialang.org/t/how-to-convert-an-svg-file-to-png-with-fixed-pixel-size-resolution/33561/2)

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [May 27, 2024, 7:33am UTC](https://discourse.julialang.org/t/svg-to-png/18643/4 "2024-05-27T07:33:27Z")

</div>

I put together a slightly more polished version of the example in the readme of the linked package, this time as a function:

```julia
using Rsvg
using Cairo

"""
	svg2png(filename_in, overwrite=false, dpi::Real=300)

Load an SVG file `filename_in`, use Rsvg.jl to render the image 
for cairo, then use Cairo.jl to write that image to a PNG file.
"""
function svg2png(filename_in, overwrite=false, dpi::Real=300)
	if !isfile(filename_in)
		error("The given filename $filename_in returned `false` when passed to `isfile`. Are you sure that the file exists?")
	end
	filename_in_noext, input_extension = splitext(filename_in)
	if input_extension != ".svg"
		error("Extension of given filename $filename_in is $filename_in_ext, where \".svg\" was expected. Aborting.")
	end
	filename_out = filename_in_noext * ".png"
	if isfile(filename_out) && !overwrite
		error("Output file $filename_out already exists, and the second positional argument `overwrite` is set to false. Aborting.")
	end
	
	r = Rsvg.handle_new_from_file(filename_in)
	Rsvg.handle_set_dpi(r, float(dpi)) # Conversion to float due to unnecessary contraints on arguments to Rsvg.handle_set_dpi
	d = Rsvg.handle_get_dimensions(r)
	cs = Cairo.CairoImageSurface(d.width,d.height,Cairo.FORMAT_ARGB32)
	c = Cairo.CairoContext(cs)
	Rsvg.handle_render_cairo(c,r)
	
	Cairo.write_to_png(cs,filename_out)
	return nothing
end

```

I then also made this function to convert any svg file in a given directory:

```julia
function convert_all_svgs(dir, overwrite=false, dpi::Real=300)
	if !isdir(dir)
		error("The given directory $dir returned false when passed to `isdir`. Are you sure the directory exists?")
	end
	map(readdir(dir, join=true)) do filename
		if splitext(filename)[2]==".svg"
			svg2png(filename, overwrite, dpi)
		end
	end
end

```
