Super fast to achieve svg to png, jpg and other formats

svg is a graphic format used to describe two-dimensional vector graphics. It has many advantages, but sometimes not all scenes support svg, so at this time, we need to convert svg images to png and other formats.

Of course, there are many ways to convert svg to png. For example, you can use online web tools, or you can use terminal commands. Here we mainly design the use of terminal commands for conversion processing.

svgexport tool

Installation is easy

 1 
npm install svgexport -g

How to use

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 
svgexport <input file> <output file> <options> svgexport <datafile> <options> [ <format> ] [ <quality> ] [ <input viewbox> ] [ <output size> ] [ <resize mode> ] [ <styles> ] <format> png|jpeg|jpg If not specified, it will be inferred from output file extension or defaults to "png" . <quality> 1%-100% <input viewbox> <left>:<top>:<width>:<height>|<width>:<height> If input viewbox is not specified it will be inferred from input file. <output size> <scale>x|<width>:<height>|<width>:|:<height> If output size is specified as width:height, <viewbox mode> is used. <viewbox mode> crop|pad Crop ( slice ) or pad ( extend ) input to match output aspect ratio, default mode is "crop" . <datafile> Path of a JSON file with following content: [ { "input" : [ "<input file>" , "<option>" , "<option>" , ... ] , "output" : [ [ "<output file>" , "<option>" , "<option>" , ... ] ] } , ... ] Input file options are merged with and overridden by output file options. Instead of a JSON file, a Node module which exports same content can be provided.

Conversion example

Scale up by 1.5x

 1 
svgexport input.svg output.png 1.5x

Scaled proportionally, specifying a width of 32px

 1 
svgexport input.svg output.png 32:

Set the width and height (32px:54px) for scaling

 1 
svgexport input.svg output.png 32:54

Set JPEG output quality

 1 
svgexport input.svg output.jpg 80%

Batch conversion script

 1 2 3 4 5 6 7 8 9 10 11 
#!/usr/bin/env ruby # encoding: utf-8 dir = ARGV [ 0 ] Dir . entries ( dir ) . select { | f | f . end_with? '.svg' } . each { | f | newFile = f . gsub '.svg' , '.png' puts newFile system "cd #{ dir } && svgexport #{ f } #{ newFile } 120:120" } 

droidyue_gzh_green_png.png

This article is reproduced from: https://droidyue.com/blog/2022/04/25/convert-svg-to-jpg-or-png-in-a-fast-way/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment