pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

Pyvips fetch returns bad image on .svs files

Open idc9 opened this issue 6 months ago • 4 comments

Reading in an image from a .svs file via pyvips fetch returns a completeley messed up image. Reading in with the crop method does return the correct image. See example below.

Example

The test image can be downloaded from here.

from openslide import open_slide
import pyvips
import numpy as np
import matplotlib.pyplot as plt

fpath = 'CMU-1.svs'

os_wsi = open_slide(fpath)

# each of these fail
# location = (0, 0)
# location = (2300, 1500)
# location = (2300, 15000)
# location = (23000, 15000)
location = (4000, 20000)

size = (256, 256)
level = 0

# read openslide patch
patch_os = os_wsi.read_region(location=location, size=size, level=level).convert('RGB')
patch_os = np.array(patch_os)

# read patch via crop
vips_image = pyvips.Image.new_from_file(vips_filename=fpath, level=level)
patch_crop = vips_image.crop(location[0], location[1], size[0], size[1])
patch_crop = patch_crop.numpy()[:, :, 0:3]


# read patch via fetch
bands = 3
vips_image = pyvips.Image.new_from_file(vips_filename=fpath, level=level)
region = pyvips.Region.new(vips_image)
patch_fetch = region.fetch(location[0], location[1], size[0], size[1])
patch_fetch = np.ndarray(buffer=patch_fetch,
                         dtype=np.uint8,
                         shape=(size[1], size[0], bands))

# do they mach?
print('fetch vs openslide', np.allclose(patch_fetch, patch_os), (patch_fetch - patch_os).mean())
print('crop vs openslide',np.allclose(patch_crop, patch_os), (patch_crop - patch_os).mean())

fetch vs openslide False 121.11172485351562
crop vs openslide True 0.0

Lets see what these look like

# Let's see what they look like
plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1)
plt.imshow(patch_os)
plt.title('openslide')
plt.axis('off')


plt.subplot(1, 3, 2)
plt.imshow(patch_crop)
plt.title('crop')
plt.axis('off')


plt.subplot(1, 3, 3)
plt.imshow(patch_fetch)
plt.title('fetch')
plt.axis('off')

Screenshot 2024-02-08 at 6 16 34 PM

System details

import platform; print(platform.platform())
import sys; print('python', sys.version)
import pyvips; print('pyvips', pyvips.__version__)
import openslide; print('openslide', openslide.__version__)
macOS-10.16-x86_64-i386-64bit
python 3.8.18 (default, Sep 11 2023, 08:17:33) 
[Clang 14.0.6 ]
pyvips 2.2.1
openslide 1.3.0

I installed pyvips using conda install.

idc9 avatar Feb 08 '24 23:02 idc9