SHARE
TWEET

Untitled

a guest Apr 21st, 2017 39 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from openslide import OpenSlide, OpenSlideUnsupportedFormatError, OpenSlideError
  2. from openslide.deepzoom import DeepZoomGenerator
  3.  
  4.  
  5. class SlideImage(object):
  6.     def __init__(self, filename):
  7.         self.filename = filename
  8.  
  9.         try:
  10.             self.osr = OpenSlide(self.filename)
  11.         except OpenSlideUnsupportedFormatError:
  12.             print 'Format of file {0} is not supported by openslide'.format(self.filename)
  13.             raise
  14.         except OpenSlideError:
  15.             print 'Unknown Openslide error'
  16.             raise
  17.  
  18.         self.zoom = DeepZoomGenerator(self.osr, tile_size=254, limit_bounds=True)
  19.         print 'Tiles in image: {0}'.format(self.zoom.tile_count)
  20.         print 'Tile levels: {0}'.format(self.zoom.level_count)
  21.         print 'Tile dimensions: {0}'.format(self.zoom.level_dimensions)
  22.  
  23.     def get_image_size(self):
  24.         """
  25.         Get the size of the image.
  26.         Same as the size of the image at level 0.
  27.         """
  28.         return self.osr.dimensions
  29.  
  30.     def get_tile(self, z, coord):
  31.         """
  32.         Get a tile at zoom level z, and coordinates coord (tuple containing (x, y)).
  33.         The tile is returned as an RGB PIL.Image.Image object.
  34.         """
  35.         image = self.zoom.get_tile(z, coord)
  36.         return image
  37.  
  38.  
  39. if __name__ == "__main__":
  40.  
  41.     # Path to image as argument, extension can be .tif, .tiff, .vms and more.
  42.     osr = SlideImage("/somedir/someimage.vms")
  43.     osr.get_tile(11, (0, 0)).show()
RAW Paste Data
Top