Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: October 10, 2024
Converting Scalable Vector Files (SVG) to the Portable Network Graphics (PNG) format is a common task when working with vector and raster images in Linux.
In this tutorial, we’ll discuss tools like Inkscape, ImageMagick, and SVG Export to convert SVG to PNG. These tools offer a simple and efficient method for converting both single and multiple files.
For most tools, we use the apt package manager to install them on Ubuntu. Before we dive into the conversion process, let’s first understand the SVG and PNG formats.
SVG and PNG are two popular image file formats used in various applications.
SVG is a vector format that uses mathematical equations to draw shapes without losing quality. Therefore, SVG is ideal for graphics that require scaling, such as logos, icons, and infographics.
PNG, on the other hand, uses a grid (or raster) of pixels to create images. This makes PNG suitable for images with many colors and details such as photographs, screenshots, and digital artwork.
Inkscape is a powerful, open-source vector graphics editor widely used for creating and editing SVG files. Inkscape supports various file formats and offers features like object manipulation, path editing, and text support.
To use Inkscape, we first install it:
$ sudo apt install inkscape
At this point, Inkscape should be available.
To convert a single SVG to PNG, we can use the –export-filename option with the respective extension:
$ inkscape --export-filename=output.png input.svg
Background RRGGBBAA: ffffff00
Area 0:0:787.909:942.681 exported to 788 x 943 pixels (96 dpi)
Here, we convert input.svg to a PNG file named output.png. Moreover, the output indicates that the background color is fully transparent by default. Additionally, we can see the total dimension and the resolution.
Converting multiple SVG files can be time-consuming. Fortunately, we can use Inkscape to perform batch conversions efficiently through the command line:
$ for file in *.svg; do inkscape --export-filename="${file%.svg}.png" "$file"; done
Now let’s understand the command:
This command processes each SVG file individually, converting it into PNG format.
In addition to converting files, we can use Inkscape to customize the appearance of the resulting images. For example, we can change the background color to white using:
$ inkscape --export-background=white --export-filename=output.png image1.svg
Another important aspect is the resolution that we can modify by adjusting the dots per inch (dpi) value:
$ inkscape --export-dpi=300 --export-filename=output.png image1.svg
Similarly, we can resize the image by specifying a width:
$ inkscape --export-width=800 --export-filename=resize_output.png image1.svg
This command exports the image with a width of 800 pixels, and the height is automatically adjusted to maintain the aspect ratio.
ImageMagick is another popular tool that we can use to transform image files in various formats including SVG.
Again, ImageMagick needs to be installed first:
$ sudo apt install imagemagick
Once installed, we can use ImageMagick for file conversion.
Converting a single SVG to PNG using ImageMagick is straightforward:
$ convert input.svg output.png
The resulting PNG image retains the original dimensions and resolution of the SVG file, with a default white background. Again, the extension decides the format.
To convert multiple SVG files to PNG in bulk, we can utilize the mogrify command:
$ mogrify -format png *.svg
This command processes all files with the SVG extension, converting them to PNG format while preserving their original filenames.
ImageMagick provides various options to fine-tune the output image.
For instance, we can modify the background to be transparent:
$ convert -background transparent input.svg output.png
We can also adjust the resolution by specifying the density:
$ convert -density 300 input.svg output.png
Additionally, we can scale the image by specifying a width and height to resize:
$ convert -resize 800x600 input.svg output.png
Alternatively, we can specify only the width, enabling the height to adjust automatically.
The SVG Export is a command-line tool specifically designed to export SVG files to various formats.
The Node Package Manager (NPM) is necessary to be installed first before installing SVG Export:
$ sudo apt install npm && sudo npm install -g svgexport
Thus, we should now have both NPM and svgexport.
For single-file conversion, we can use the direct syntax:
$ svgexport input.svg output.png
The resulting image maintains its original dimension and has no compression, preserving its quality. Further, the background color is transparent similar to Inkscape.
For batch conversion, we can again use a shell loop:
$ for file in *.svg; do svgexport "$file" "${file%.svg}.png"; done
Again, a similar approach is used to process multiple SVG files but with the svgexport tool.
To customize the image, we can use different options.
For example, let’s change the background:
$ svgexport input.svg output.png --background "white"
To modify the resolution, we use –dpi:
$ svgexport input.svg output.png --dpi 300
In addition, we can alter the image width:
$ svgexport input.svg output.png --width 800
Similar to the earlier example, the output image has a width of 800 pixels while keeping its proportions intact.
In this article, we’ve explored three effective tools – Inkscape, ImageMagick, and SVG Export, for converting SVG files to PNG using the terminal.
Inkscape is ideal for vector graphics editing and conversion, while ImageMagick provides various image manipulation options. SVG Export is a lightweight tool for exporting SVG to other formats.
Depending on the specific requirements, we can choose the best tool based on its strengths and use cases.