Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openslide: select best input level based on output dimensions #2222

Closed
TAYTS opened this issue May 24, 2020 · 13 comments
Closed

Openslide: select best input level based on output dimensions #2222

TAYTS opened this issue May 24, 2020 · 13 comments

Comments

@TAYTS
Copy link

TAYTS commented May 24, 2020

I am trying to generate thumbnail file from a large openslide format image(~2Gb), but the resize takes forever to run, not sure if I did it wrongly. Below are my code for trying to generate the thumbnails.

I manage to use vipsthumbnail to generate the thumbnail very fast(instant) and I am using the global libvips.

sharp('test.scn', {limitInputPixels: false})
  .resize(500, 500)
  .png()
  .toFile('output.png')
  .then(info => console.log(info))
  .catch(err => console.log(err))
@TAYTS TAYTS added the question label May 24, 2020
@lovell
Copy link
Owner

lovell commented May 25, 2020

Are you able to provide a sample image?

What versions of sharp, libvips and openslide are you using? On what platform?

@kleisauke
Copy link
Contributor

Note that vipsthumbnail uses shrink-on-load for Openslide images since version 8.8, which might explain the speed improvement you noticed. See:
libvips/libvips@22ba910
libvips/libvips#1149

@TAYTS
Copy link
Author

TAYTS commented May 25, 2020

@lovell Sorry for missing out the details.

sharp: 0.25.3
libvips: 8.9.2
openslide: 3.4.1
platform: macOS

I am afraid I am not able to provide the actual image but here is a link to a sample data:
http://openslide.cs.cmu.edu/download/openslide-testdata/Leica/Leica-3.scn

@TAYTS
Copy link
Author

TAYTS commented May 25, 2020

@kleisauke Is the shrink-on-load currently supported by sharp or I have missed out the resize configuration as the libvips version stated in the package.json is 8.9.1?

@TAYTS
Copy link
Author

TAYTS commented May 27, 2020

Is the fast thumbnail generation supported in the sharp as I can't seem to find the documentation about it?

@lovell
Copy link
Owner

lovell commented May 28, 2020

Commit f5f1654 on the work-in-progress openslide-shrink-on-load branch attempts to add this feature.

@TAYTS Please can you test this.

npm install lovell/sharp#openslide-shrink-on-load

@TAYTS
Copy link
Author

TAYTS commented May 31, 2020

@lovell I got [Error: webp: unable to parse image] after using the new version even with clean install. Do I need to configure the file type specifically? I am testing using the sample data from http://openslide.cs.cmu.edu/download/openslide-testdata/Leica/

@lovell
Copy link
Owner

lovell commented May 31, 2020

I think the Leica-3.scn sample fails due to openslide/openslide#189 - or are you referring to a different image?

@TAYTS
Copy link
Author

TAYTS commented May 31, 2020

@lovell I have tried on the smaller image Leica-1.scn and it works on the current stable version (able to generate the output), but I get the same error (unable to parse image) for the new version.

@lovell
Copy link
Owner

lovell commented Jun 2, 2020

Thank you. It looks like shrink-on-load for openslide input is a little more complex than providing a scaling factor. We need to calculate the best level to extract, based on its dimensions vs desired dimensions. Here's the relevant metadata from the Leica-1.scn sample:

openslide.level-count: 5
openslide.level[0].downsample: 1
openslide.level[0].height: 153470
openslide.level[0].width: 53130
openslide.level[1].downsample: 3.9998986524159981
openslide.level[1].height: 38368
openslide.level[1].width: 13283
openslide.level[2].downsample: 15.998992404088622
openslide.level[2].height: 9592
openslide.level[2].width: 3321
openslide.level[3].downsample: 63.927109191868013
openslide.level[3].height: 2401
openslide.level[3].width: 831
openslide.level[4].downsample: 255.3952147062588
openslide.level[4].height: 601
openslide.level[4].width: 208

Let's tag this as an enhancement for now.

@lovell lovell changed the title Generate thumbnail for openslide format Openslide: select best input level based on output dimensions Jun 2, 2020
@lovell
Copy link
Owner

lovell commented Jun 4, 2020

The "quickest" (to implement) solution to help with this is to allow the extraction of a specific level, rather than the current behaviour of always using the first/largest layer.

The first step in commit 760550c exposes levels via metadata, e.g. for the Leica-1.scn example:

{
  format: 'openslide',
  width: 53130,
  height: 153470,
  space: 'srgb',
  channels: 4,
  depth: 'uchar',
  isProgressive: false,
  levels: [
    { width: 53130, height: 153470 },
    { width: 13283, height: 38368 },
    { width: 3321, height: 9592 },
    { width: 831, height: 2401 },
    { width: 208, height: 601 }
  ],
  hasProfile: false,
  hasAlpha: true
}

The next step is to allow the required level to be specified in the sharp() constructor options.

It will then at least be possible for someone to select the most relevant level based on their desired output dimensions.

@lovell
Copy link
Owner

lovell commented Jun 6, 2020

Commit 7f142bd adds the level constructor option to use a specific level.

sharp('Leica-1.scn', {
  level: 4,
  limitInputPixels: false
})
...

This will be in v0.25.4.

@lovell lovell added this to the v0.25.4 milestone Jun 6, 2020
@lovell
Copy link
Owner

lovell commented Jun 12, 2020

v0.25.4 is now available.

@lovell lovell closed this as completed Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants