def tile2WebPNG(self, xdim, ydim, tile): """Create PNG Images and write to cache for the specified tile""" # Check if it is mcfc tile if self.colors is not None: return mcfcPNG(tile, self.colors, enhancement=4.0) # If it is not a mcfc tile else: ch = self.ds.getChannelObj(self.channels[0]) # write it as a png file if ch.getChannelType() in IMAGE_CHANNELS + TIMESERIES_CHANNELS: if ch.getChannelDataType() in DTYPE_uint8: return Image.frombuffer ( 'L', [xdim,ydim], tile.flatten(), 'raw', 'L', 0, 1 ) elif ch.getChannelDataType() in DTYPE_uint16: if ch.getWindowRange() != [0,0]: tile = np.uint8(tile) return Image.frombuffer ( 'L', [xdim,ydim], tile.flatten(), 'raw', 'L', 0, 1 ) else: outimage = Image.frombuffer ( 'I;16', [xdim,ydim], tile.flatten(), 'raw', 'I;16', 0, 1) return outimage.point(lambda i:i*(1./256)).convert('L') elif ch.getChannelDataType() in DTYPE_uint32 : return Image.fromarray( tile[0,:,:], 'RGBA') elif ch.getChannelType() in ANNOTATION_CHANNELS: tile = tile[0,:] ndlib.recolor_ctype(tile, tile) return Image.frombuffer ( 'RGBA', [xdim,ydim], tile.flatten(), 'raw', 'RGBA', 0, 1 ) else : logger.warning("Datatype not yet supported".format(ch.channel_type))
def fetch (self): """Retrieve the tile from the cache or load the cache and return""" try: # open file and return f = open(self.filename) self.db.touch (self.tkey) return f.read() except IOError: pass try: self.initForFetch() except OOBException: logger.warning("OOB request. Returning black tile. url={}".format(self.tile_url)) img = Image.new("L", (settings.TILESIZE, settings.TILESIZE)) fileobj = cStringIO.StringIO() img.save(fileobj, "PNG") fileobj.seek(0) return fileobj.read() from tasks import fetchcube # check if there is S3 backend and do the calls accordingly if self.ds.getS3Backend(): s3_backend = s3io.S3IO(self.ds, self.channels) cubedata = s3_backend.getCutout(self.cuboid_url) tile_data = cubedata[:, self.zslab_offset, : ,:] # fetchcube(self.token, self.slice_type, self.channels, self.colors, self.cuboid_url, cubedata) fetchcube.delay (self.token, self.slice_type, self.channels, self.colors, self.cuboid_url, cubedata) ch = self.ds.getChannelObj(self.channels[0]) # checking the channel type to process the data correctly if self.colors: img = mcfc.mcfcPNG (tile_data, self.colors) elif ch.getChannelType() in IMAGE_CHANNELS + TIMESERIES_CHANNELS: if ch.getChannelDataType() in DTYPE_uint8: img = Image.frombuffer('L', tile_data.shape[1:][::-1], tile_data.flatten(), 'raw', 'L', 0, 1) elif ch.getChannelDataType() in DTYPE_uint16: if ch.getWindowRange() != [0,0]: tile_data = np.uint8(tile_data) img = Image.frombuffer('L', tile_data.shape[1:][::-1], tile_data.flatten(), 'raw', 'L', 0, 1) else: img = Image.frombuffer ( 'I;16', tile_data.shape[1:][::-1], tile_data.flatten(), 'raw', 'I;16', 0, 1) img.point(lambda i:i*(1./256)).convert('L') elif ch.getChannelDataType() in DTYPE_uint32 : img = Image.fromarray( tile_data[0,:,:], 'RGBA') elif ch.getChannelType() in ANNOTATION_CHANNELS: tile_data = tile_data[0,:] ndlib.recolor_ctype(tile_data, tile_data) img = Image.frombuffer('RGBA', tile_data.shape[1:][::-1], tile_data.flatten(), 'raw', 'RGBA', 0, 1) fileobj = cStringIO.StringIO() img.save ( fileobj, "PNG" ) fileobj.seek(0) return fileobj.read() else: # call the celery process to fetch the url #fetchurl (self.token, self.slice_type, self.channels, self.colors, self.cuboid_url) fetchcube.delay (self.token, self.slice_type, self.channels, self.colors, self.cuboid_url) # fetchcube (self.token, self.slice_type, self.channels, self.colors, self.cuboid_url) logger.warning("Tile fetch {}".format(self.tile_url)) return getURL(self.tile_url).read()