Converting and resizing svgs to pngs

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
imlost123
Posts: 15
Joined: 2017-02-02T04:46:49-07:00
Authentication code: 1151

Converting and resizing svgs to pngs

Post by imlost123 »

Hi, I'm trying to batch resize and convert 1000+ svgs to pngs and I'm very confused.

Ideally I'd like to resize all the svgs to 2160pixels x 2160pixels and then convert them to png.
So far I can only convert and resize one at a time by typing the file name in manually and the end result is a mess as it's just a resized 64x64 image which looks terrible.

I imagine this is very simple but what commands would you use to do this with a few clicks? honestly I've been wrestling with his for over a day, sorry, my brain doesn't work like it used to!

Resize 1000 svgs to 2160x2160
then convert to png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

cRe: Converting and resizing svgs to pngs

Post by snibgo »

In essence, this is easy. Do it one file at a time with convert, and put that inside a shell for loop:

Code: Select all

convert in.svg -resize 2160 out.png
Or use a single mogrify:

Code: Select all

mogrify -resize 2160 -type png *.svg
However, SVGs are vector files, probably containing only vector data. If the "natural" rasterization is smaller than 2160 pixels, they will be enlarged, which may look bad. You might prefer to include "-density N" before the SVG file, where N is an integer such as 150 or 600, and depends on a few things.
snibgo's IM pages: im.snibgo.com
imlost123
Posts: 15
Joined: 2017-02-02T04:46:49-07:00
Authentication code: 1151

Re: Converting and resizing svgs to pngs

Post by imlost123 »

Thanks I ended up getting there after even more head scratching. Ran into a few problems like the transparencies turned to pure white so a bit of googling and I discovered the "-background none" option and yeah I needed to add the -density and I couldn't get your mogrify code working but I got there eventually so thanks!

the actual final command I used was
"mogrify -background none -density 5000 -format png *.svg"
I guess the density amount was a bit OTT and the svgs came out at 3555px but that was ok and I'm very happy. I almost paid someone money to do it for me!

Fascinating little bit of real software in this day and age of apps.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Converting and resizing svgs to pngs

Post by snibgo »

Good stuff.

The default density is 90 dpi. So if your first-try rasterization comes out at 64 pixels wide and you want it 2160 pixels wide, use a density of 90 * 2160/64 = 3037.5 or thereabouts. Or use 10% larger, and resise down to the exact size you want.
snibgo's IM pages: im.snibgo.com
Post Reply