Page 1 of 1

Converting and resizing svgs to pngs

Posted: 2017-02-02T05:27:38-07:00
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

cRe: Converting and resizing svgs to pngs

Posted: 2017-02-02T07:06:27-07:00
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.

Re: Converting and resizing svgs to pngs

Posted: 2017-02-02T15:44:15-07:00
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.

Re: Converting and resizing svgs to pngs

Posted: 2017-02-02T16:15:15-07:00
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.