Using setResolution, you can adjust the density prior to reading in an SVG. As the comment at the bottom notes, this is set in relation to the current resolution.
I'm trying to adjust the resolution to 300DPI. In my environment (and as is noted in the documentation), the default resolution is 90 DPI. Because of this, setResolution(300, 300) renders an image that falls short of what the geometry should be. I have to use 375,375 to fill the area as expected. However, a client's environment may be 72 DPI, because that same code caused the resulting sections to clip outside of the bounding box set for it, and setResolution(300,300) is the only way to get equivalent output.
How can I tell ImageMagick to use the proper resolution so that quality is retained and the geometry is the same, no matter which environment I am in?
Set static SVG resolution (or get resolution prior to reading)
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Set static SVG resolution (or get resolution prior to reading)
"setResolution" used before an SVG image is rasterized will tell the SVG rasterizer what resolution to use. If you don't actually care about the resolution, but you want a specific dimension (pixels width or height), you can:
1. Rasterize the SVG file. Find its resolution, width and height.
2. Calculate what resolution will give you the dimensions you want. Eg, if the result from (1) is 72 DPI and 300 pixels wide but you want 400 pixels wide, the required resolution is 72 * 400/300 = 96 DPI.
3. Rasterize again, but this time set the required resolution.
1. Rasterize the SVG file. Find its resolution, width and height.
2. Calculate what resolution will give you the dimensions you want. Eg, if the result from (1) is 72 DPI and 300 pixels wide but you want 400 pixels wide, the required resolution is 72 * 400/300 = 96 DPI.
3. Rasterize again, but this time set the required resolution.
snibgo's IM pages: im.snibgo.com
Re: Set static SVG resolution (or get resolution prior to reading)
Thanks for your response. So to be clear, the only way to do this is to test it twice? There's no built-in function to return what the SVG renderer's default DPI is?
Re: Set static SVG resolution (or get resolution prior to reading)
When you are using librsvg as your SVG render and you are on a recent version of IM you could try calling setSize before you load the image. This would allow you to specify a fixed dimension. I haven't tested this with IMagick so I am not sure if that works. Can you give it a try and let us know if it works?
Re: Set static SVG resolution (or get resolution prior to reading)
Oh wow. Actually that does seem to work. I had thought I'd tried that and it made the final output fuzzy, but it looks quite nice!
Thanks very much for the responses, I greatly appreciate it!
Thanks very much for the responses, I greatly appreciate it!
Re: Set static SVG resolution (or get resolution prior to reading)
As an update (and as you did mention), this seems to only work on versions of ImageMagick above 6.7 (My local environment, where it worked, is 6.9.5-10; the environments where this is to be run is 6.7.2-7).
Do you think there might be an equivalent way to do this with 6.7 without having to render the SVG twice?
Do you think there might be an equivalent way to do this with 6.7 without having to render the SVG twice?