Skip to content

Multiple Image extraction and merging appears unnecessarily slow #1175

Closed

Description

@CalumD

Hi @lovell,

I would like to start by saying what a fantastic find your library is - and how impressed I am by your responsiveness and willingness to help the other issues here. Having also seen how many other issues you have to deal with, I note that the opening of this issue was not a whimsical decision, but I just can't seem to find the solution to the problem, and time is becoming a bit pressing. I am working on a project for university that requires the processing (extracting of certain regions and stitching) of many thousands of images.
For reference, similar issues have been thoroughly read and tried at #405 #699 #728 #864 #886 #971

The workflow is as follows:
I have a 2D array of .PNG files on the local filesystem, which make up "tiles" of a much larger image. Many requests will be made to the node server for fast on-the-fly regions of the whole image.
I know all the x/y coordinates and offsets of all base tiles, as well as the region to be extracted, and therefore the area and coordinates of the overlap to be created as a new image.
The regions to be extracted and stitched together will not overlap each other but need to be stitched side by side.
All base tiles are <1Mb in size with most ~600Kb

Approaches considered:

  1. Use sharp to load and extract the desired specific region from each base tile image, and place in a 2D Buffer object. step through to append all buffers in each row, then step through to combine all row buffers into final image.
  2. Use sharp to stitch all of the base tiles into a single large image, then do a single extraction of the desired region.
  3. Start with an empty image of the final resolution, and iterate over all related tiles adding to the canvas as I go.

I have managed to use the code and suggestions provided in the various comments mentioned above, to create a working solution but it is extremely slow - given what "little" work is actually required specifically in the case of approach 2. (The most primary reason I chose sharp was due to the toted speed up it provides over others). In working solutions so far, calls to .extend(), .toBuffer() and overlayWith() are needed but results have to be wrapped in new instances of a SharpInstance, which is where I have convinced myself from basic testing, the significant slowdown is taking place.
These are needed in order to avoid the main issue: using multiple .overlayWith() calls on the same instance only applies the last operation, and either deletes or ignores the work of previous calls.

The reason I say this appears slow and the key motivation behind this issue submit, is that the particular test case I have been working with only contains 12 base tiles, totalling 7.1Mb with the final image only resulting in 2697px by 3125px at 5.6Mb, yet it takes ~1 minute to complete with all previous working attempts.

Any input would be much appreciated - thanks,
Calum.

Activity

  1. lovell commented on Mar 31, 2018

    @lovell
    Owner

    If "time is becoming a bit pressing" then I'd use vips at the command line for this.

    http://jcupitt.github.io/libvips/API/current/libvips-conversion.html#vips-arrayjoin

    To stitch 4 images in a 2x2 arrangement:

    vips arrayjoin "1.png 2.png 3.png 4.png" out.png --across 2
  2. CalumD commented on Apr 2, 2018

    @CalumD
    Author

    Ah, yes perhaps I was looking too tunnel vision at it,
    I used vips directly and noticed a speed up of around 10x,

    Thanks for the speedy reply and suggestion.

  3. lovell commented on Apr 2, 2018

    @lovell
    Owner

    Great, thanks for confirming.

    The work for #728 will make sharp a lot faster as it will remove the (un)premultiply round trip each time.

  4. mariosvlad commented on Apr 7, 2018

    @mariosvlad

    hello @lovell , we are using sharp in an electron app and we have a similar scenario, is there an easy way to use libvips from the command line (node.js child_process) as you suggested but using the binaries already bundled with sharp?
    thanks!

  5. Repository owner locked and limited conversation to collaborators on Apr 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions