pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

Reading in image a second time breaks on qptiff file

Open idc9 opened this issue 6 months ago • 6 comments

Reading in an image from a qptiff file breaks when I run the same code twice.

If I run the below once it works. If I run it again (I'm using a jupyter notebook) it throws an error. If I run it a third time it throws a different error.

I'm not able to post the image publicly but I've emailed it to @jcupitt.

import pyvips
import matplotlib.pyplot as plt
fpath = 'myfile.qptiff'

page_idx = 17
location = (0, 0)
size = (375, 483)  # this is the entire image

# read patch via crop
vips_image = pyvips.Image.new_from_file(vips_filename=fpath, page=page_idx)
patch_crop = vips_image.crop(location[0], location[1], size[0], size[1])
patch_crop = patch_crop.numpy()
plt.imshow(patch_crop)

Error message after second call

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
Cell In[4], line 11
      9 vips_image = pyvips.Image.new_from_file(vips_filename=fpath, page=page_idx)
     10 patch_crop = vips_image.crop(location[0], location[1], size[0], size[1])
---> 11 patch_crop = patch_crop.numpy()# [:, :, 0:3]
     12 plt.imshow(patch_crop)

File ~/anaconda3/envs/cpath/lib/python3.8/site-packages/pyvips/vimage.py:1273, in Image.numpy(self, dtype)
   1252 def numpy(self, dtype=None):
   1253     """Convenience function to allow numpy conversion to be at the end
   1254     of a method chain.
   1255 
   (...)
   1271           strings to numpy dtype strings.
   1272     """
-> 1273     return self.__array__(dtype=dtype)

File ~/anaconda3/envs/cpath/lib/python3.8/site-packages/pyvips/vimage.py:1234, in Image.__array__(self, dtype)
   1208 """Conversion to a NumPy array.
   1209 
   1210 Args:
   (...)
   1229 See Also `Image.new_from_array` for the inverse operation. #TODO
   1230 """
   1231 import numpy as np
   1233 arr = (
-> 1234     np.frombuffer(self.write_to_memory(),
   1235                   dtype=FORMAT_TO_TYPESTR[self.format])
   1236     .reshape(self.height, self.width, self.bands)
   1237 )
   1239 if self.bands == 1:
   1240     # flatten single-band images
   1241     arr = arr.squeeze(axis=-1)

File ~/anaconda3/envs/cpath/lib/python3.8/site-packages/pyvips/vimage.py:944, in Image.write_to_memory(self)
    942 pointer = vips_lib.vips_image_write_to_memory(self.pointer, psize)
    943 if pointer == ffi.NULL:
--> 944     raise Error('unable to write to memory')
    945 pointer = ffi.gc(pointer, glib_lib.g_free)
    947 return ffi.buffer(pointer, psize[0])

Error: unable to write to memory
  tiff2vips: out of order read -- at line 483, but line 0 requested

Error message after third call

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
Cell In[5], line 11
      9 vips_image = pyvips.Image.new_from_file(vips_filename=fpath, page=page_idx)
     10 patch_crop = vips_image.crop(location[0], location[1], size[0], size[1])
---> 11 patch_crop = patch_crop.numpy()
     12 plt.imshow(patch_crop)

File ~/anaconda3/envs/cpath/lib/python3.8/site-packages/pyvips/vimage.py:1273, in Image.numpy(self, dtype)
   1252 def numpy(self, dtype=None):
   1253     """Convenience function to allow numpy conversion to be at the end
   1254     of a method chain.
   1255 
   (...)
   1271           strings to numpy dtype strings.
   1272     """
-> 1273     return self.__array__(dtype=dtype)

File ~/anaconda3/envs/cpath/lib/python3.8/site-packages/pyvips/vimage.py:1234, in Image.__array__(self, dtype)
   1208 """Conversion to a NumPy array.
   1209 
   1210 Args:
   (...)
   1229 See Also `Image.new_from_array` for the inverse operation. #TODO
   1230 """
   1231 import numpy as np
   1233 arr = (
-> 1234     np.frombuffer(self.write_to_memory(),
   1235                   dtype=FORMAT_TO_TYPESTR[self.format])
   1236     .reshape(self.height, self.width, self.bands)
   1237 )
   1239 if self.bands == 1:
   1240     # flatten single-band images
   1241     arr = arr.squeeze(axis=-1)

File ~/anaconda3/envs/cpath/lib/python3.8/site-packages/pyvips/vimage.py:944, in Image.write_to_memory(self)
    942 pointer = vips_lib.vips_image_write_to_memory(self.pointer, psize)
    943 if pointer == ffi.NULL:
--> 944     raise Error('unable to write to memory')
    945 pointer = ffi.gc(pointer, glib_lib.g_free)
    947 return ffi.buffer(pointer, psize[0])

Error: unable to write to memory

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 09 '24 15:02 idc9