user354134
user354134

Reputation:

Crop and scale SVG to PNG using ImageMagick, without pixellation

I have an SVG with many polygons:

https://github.com/barrycarter/bcapps/blob/master/sample-data/current-temps.svg

that looks somewhat like this:

enter image description here

Cropping this and converting to PNG works fine:

convert -crop 100x100+200+150 current-temps.svg /tmp/exhb2.png

enter image description here

Cropping and scaling, however, fails:

convert -crop 100x100+200+150 -scale 1000x750 current-temps.svg /tmp/exhb3.png

enter image description here

How do I make ImageMagick "zoom into" the SVG before cropping?

(I realize ImageMagick can only read, not write, the SVG format, but it should still be able to do what I want?)

EDIT/SOLVED:

Thanks, robermorales.

inkscape -z -e out4.png -w 1000 -h 1000 -a 200:200:400:400 current-temps.svg 

(for example) worked like a charm.

I also realized that copying the SVG, tweaking the transform line:

<g transform="scale(3,3) rotate(-90) translate(-90,180)"> 

and then converting to PNG is another solution.

Upvotes: 7

Views: 5097

Answers (3)

fmw42
fmw42

Reputation: 53174

Proper ImageMagick syntax here is to read the input, then crop, then resize. See http://www.imagemagick.org/Usage/basics/#why Though that is not likely the issue. The issue is that -scale will replicated pixels and so make it blocky. You should replace -scale with -resize. That will be smoother, but blurry for the amount of magnification you are requesting. Try this command:

convert current-temps.svg -crop 100x100+200+150 -resize 1000x750 exhb3.png

Upvotes: 0

Mage Bay
Mage Bay

Reputation: 21

Don't crop an SVG into PNG. You can use viewBox to re-define the crop area then convert that SVG into PNG in highest solution as possible. Check this post https://www.sarasoueidan.com/blog/svg-coordinate-systems/ explain what is viewBox and you will got my idea.

Upvotes: 2

robermorales
robermorales

Reputation: 3553

Try doing scale before crop.

However, doing that using inkscape cli is easier.

Sorry for no links, afk

Upvotes: 5

Related Questions