Sharp: Resizing SVGs and converting them to PNGs takes hours for some images

Created on 8 May 2018  路  10Comments  路  Source: lovell/sharp

I am using sharp to resize SVG images and output them as PNG. This works fine for a lot of images, but is painfully slow for some others.

I set up a sample project that demonstrates this behaviour: https://github.com/nikwen/sharp-demo

An example image that is very slow is the following: https://openclipart.org/download/172385/1347916868.svg

This is the sharp call I am doing:

sharp('./image.svg')
    .resize(192, 192)
    .background({r: 0, g: 0, b: 0, alpha: 0})
    .embed()
    .png()
    .toBuffer()

On my machine, the aforementioned sharp call with the given image takes 1 hour 55 minutes 46.02 seconds to complete.

I noticed that it takes less time for sizes that are powers of 2 (such as 512x512), but it still takes too long.

System:

  • CPU: Intel Core i5-4570 CPU @ 3.20GHz 脳 4
  • RAM: 8GB
  • OS: Fedora 27 (Linux kernel 4.16.6)
  • Node.js: 8.11.1
bug ready-to-ship

Most helpful comment

sharp v0.21.0 is now available with a prebuilt libvips v8.7.0 that contains the upstream fix.

All 10 comments

Hello, thanks for the detailed report. It looks like this is due to some serious over-computation between librsvg and libvips, which results in the whole SVG being rendered thousands of times.

Using rsvg at the command line suggests this operation should be taking 3-4 seconds.

$ time rsvg-convert image.svg >output.png

real    0m3.351s
user    0m3.320s
sys     0m0.032s

I've identified a possible fix to libvips for this that I'll submit upstream in the next day or so.

@lovell Thank you very much for your quick reply and the work you put into this.
I'm really looking forward to having this issue resolved. :)

This has been addressed in libvips - see https://github.com/jcupitt/libvips/pull/969

The future sharp v0.21.0 will probably ship with the future libvips v8.7.0 that will include this change.

@lovell You are awesome! Thank you so much.
It's rare to see a maintainer that helpful! :)

@lovell Any idea when v0.21.0 would be released? Our product has a requirement for resizing SVG images on the fly but we are stuck due to this issue. 馃槥

@gkcgautam This is dependent on the yet-to-be-released libvips v8.7.0, hence the blocked-upstream-dependency label.

@gkcgautam An upstream release candidate has been released: https://github.com/jcupitt/libvips/releases

sharp v0.21.0 is now available with a prebuilt libvips v8.7.0 that contains the upstream fix.

sharp v0.21.0 is now available with a prebuilt libvips v8.7.0 that
contains the upstream fix.
Thank you so much, Lovell!

@lovell Just to give you an impression of how much of a difference this update made to me: My image processing task went from 2.5 hours to 17 seconds! Thanks a lot! :tada:

Was this page helpful?
0 / 5 - 0 ratings