libvips / pyvips

python binding for libvips using cffi
MIT License
649 stars 50 forks source link

How to read mrxs format image and choose certain dimension level. Then cut them into desire tiles? #431

Open HanYanxi opened 1 year ago

HanYanxi commented 1 year ago

import pyvips svs_file = 'file.mrxs'

img = pyvips.Image.new_from_file(svs_file, level=0)

img2 = img.crop(x1, y1, h, w) img2 = np.asarray(img2, dtype=np.uint8)

I got an attribute error from "level=0"

jcupitt commented 1 year ago

Hello @HanYanxi ,

I would guess your libvips has been built without openslide support. You should see:

$ vipsheader Mirax2.2-1.mrxs
Mirax2.2-1.mrxs: 101832x219976 uchar, 4 bands, srgb, openslideload

ie. vips has detected an openslide format.

HanYanxi commented 1 year ago

Hello @jcupitt, Thank you for your reply. I checked it. I don't have openslide support. This is what I get. May I ask how can I get the openslide support? image

jcupitt commented 1 year ago

On Windows you need the all binary:

https://github.com/libvips/build-win64-mxe/releases/download/v8.14.5/vips-dev-w64-all-8.14.5.zip

HanYanxi commented 1 year ago

Thank you. It works. But how can I check how many dimension levels the mrxs image has? And how can I know the shape of each level? I cannot find the document for it.

jcupitt commented 1 year ago

It's in the image properties. Try:

$ vipsheader -a Mirax2.2-1.mrxs 
Mirax2.2-1.mrxs: 101832x219976 uchar, 4 bands, srgb, openslideload
width: 101832
height: 219976
bands: 4
format: uchar
coding: none
interpretation: srgb
xoffset: 0
yoffset: 0
xres: 4275.77
yres: 4267.47
filename: Mirax2.2-1.mrxs
...
openslide.level-count: 10
openslide.level[0].downsample: 1
openslide.level[0].height: 219976
openslide.level[0].width: 101832
openslide.level[1].downsample: 2
openslide.level[1].height: 109988
openslide.level[1].width: 50916
openslide.level[2].downsample: 4
openslide.level[2].height: 54994
openslide.level[2].width: 25458
...

So from Python (for example) you can use:

n_levels = image.get("openslide.level-count")
HanYanxi commented 1 year ago

Thank you! I used "n_levels = image.get("openslide.level-count")" in Python only got a number, like 10. Is that right?

jcupitt commented 1 year ago

It could be, mrxs files have a lot of levels.