question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SVG to PNG conversion produces pixelated output with height and width values

See original GitHub issue

When resizing svg files and converting them to png, the output is really pixelated if

  • the svg file has a height and a width specified in pixels and
  • the size it should be resized to is larger than these values.

I would expect svgs to be resized properly, no matter if a height and a width are specified.

Sample project

https://github.com/nikwen/sharp-demo/tree/svg-resize-height-width (make sure you are on the svg-resize-height-width branch)

Sample image conversion

Source (32x32px): https://github.com/nikwen/sharp-demo/blob/svg-resize-height-width/image.svg Result (512x512px): https://github.com/nikwen/sharp-demo/blob/svg-resize-height-width/out.png

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
lovellcommented, Oct 19, 2018

Hello, did you see the density property to set the DPI at which to render vector images?

- sharp('./image.svg')
+ sharp('./image.svg', { density: 300 })
3reactions
mynameisrufuscommented, Jul 24, 2019

This is some super rough code that seems to work ok:

  let instance = sharp(params.buffer)

  const metadata = await instance.metadata()

  if (metadata.format === 'svg' && width && metadata.width && width > metadata.width) {
    let density = (72 * width) / metadata.width
    instance = sharp(buffer, { density: density > 2400 ? 2400 : density})
  }

  if (metadata.format === 'svg' && height && metadata.height && height > metadata.height) {
    let density = (72 * height) / metadata.height
    instance = sharp(buffer, { density: density > 2400 ? 2400 : density})
  }

  return instance.resize(width, height)
Read more comments on GitHub >

github_iconTop Results From Across the Web

In python convert SVG to PNG while resizing and increasing ...
So I need to take an SVG and produce a PNG with width w and height h and specific dpi. I want to...
Read more >
ImageMagick Convert: How to produce sharp resized PNG ...
The problem is the width="16px" height="16px" in the SVG file. Try changing the ImageMagick command to convert -resize 3200% pinterest.svg pinterest.png for a ......
Read more >
SVG to PNG Converter - CloudConvert
Convert scalable vector images to PNG with CloudConvert! Our conversion technology preserveres transparency and allows to scale lossless the vector to any ...
Read more >
Export SVG to PNG in Inkscape - Davies Media Design
In this Inkscape tutorial, I show you how to export svg files to png images using Inkscape! Convert your vector graphics to PNG...
Read more >
Converting dynamic SVG to PNG with node.js, d3 and ...
One option to provide backwards compatibility is to rasterize SVG to a static image. This saves you from creating a pixel-perfect alternate ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found