×

How to Convert SVG to PNG in Linux Terminal

How to Convert SVG to PNG in Linux Terminal


SVG (Scalable Vector Graphics) files are commonly used for logos, icons, and illustrations because they can be resized without losing quality.

However, sometimes you need to convert SVG files to a PNG (Portable Network Graphics) format for better compatibility with websites or applications.

In Linux, there are several ways to do this conversion easily, using both command-line tools and graphical applications.

1. Using Inkscape

Inkscape is a powerful, open-source vector graphics editor that supports the SVG format and can easily be used to convert SVG files to PNG format.

If Inkscape is not installed on your system, you can install it using the following commands based on your Linux distribution:

sudo apt install inkscape         [On Debian, Ubuntu and Mint]
sudo yum install inkscape         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/inkscape  [On Gentoo Linux]
sudo apk add inkscape             [On Alpine Linux]
sudo pacman -S inkscape           [On Arch Linux]
sudo zypper install inkscape      [On OpenSUSE]    
sudo pkg install inkscape         [On FreeBSD]

Once installed, you can use the following command to convert the SVG file to PNG.

inkscape input.svg --export-type=png --export-filename=output.png --export-dpi=300

2. Using ImageMagick

ImageMagick is a versatile tool that can convert images between different formats, including SVG to PNG from the command line.

If ImageMagick is not installed on your system, you can install it using the following commands based on your Linux distribution:

sudo apt install imagemagick         [On Debian, Ubuntu and Mint]
sudo yum install ImageMagick         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a sys-apps/imagemagick  [On Gentoo Linux]
sudo apk add imagemagick             [On Alpine Linux]
sudo pacman -S imagemagick           [On Arch Linux]
sudo zypper install imagemagick      [On OpenSUSE]    
sudo pkg install imagemagick         [On FreeBSD]

Once ImageMagick is installed, you can convert SVG files to PNG by running:

convert input.svg output.png

If you want to set a custom resolution for the output PNG file, use the -density option.

convert -density 300 input.svg output.png

3. Using rsvg-convert (Part of librsvg)

rsvg-convert is a command-line tool that is part of the librsvg package, which is lightweight and specifically designed for converting SVG files.

If rsvg-convert: is not installed on your system, you can install it using the following commands based on your Linux distribution:

sudo apt install librsvg2-bin         [On Debian, Ubuntu and Mint]
sudo yum install librsvg2-tools       [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
sudo emerge -a gnome-base/librsvg     [On Gentoo Linux]
sudo apk add librsvg                  [On Alpine Linux]
sudo pacman -S librsvg                [On Arch Linux]
sudo zypper install librsvg2-tools    [On OpenSUSE]    
sudo pkg install librsvg2             [On FreeBSD]

Once rsvg-convert is installed, you can convert an SVG file to PNG by running the following command:

rsvg-convert -o output.png input.svg

You can specify the width or height of the output image using -w (width) or -h (height):

rsvg-convert -w 800 -h 600 -o output.png input.svg

4. Batch Conversion of SVG to PNG

If you have multiple SVG files and want to convert them all to PNG at once, you can use a simple shell loop.

Using Inkscape:

for file in *.svg; do
  inkscape "$file" --export-type=png --export-filename="${file%.svg}.png"
done

Using ImageMagick:

for file in *.svg; do
  convert "$file" "${file%.svg}.png"
done

This loop will convert all .svg files in the current directory to .png.

Conclusion

Converting SVG to PNG in Linux is a simple task that can be done using various tools. Whether you prefer a graphical tool like Inkscape, the flexibility of ImageMagick, or a quick command-line tool like rsvg-convert, Linux provides several easy methods for this conversion.



Source link