77import os
88import os .path
99import math
10+ import numpy as np
1011from PIL import Image as PILImage , TiffImagePlugin
1112import requests
1213from time import time
@@ -84,11 +85,10 @@ def __init__(self, user, project_id, stack_mirror_ids, x_min,
8485 self .single_channel = single_channel
8586 self .output_path = output_path
8687
87- def get_tile_path (self , stack , mirror , tile_coords ) -> str :
88- """ This method will be used when get_tile_path is called after the
89- job has been initialized.
90- """
88+ def get_source_metadata (self , stack , mirror , tile_coords ):
9189 tile_source = self .stack_tile_sources [stack .id ]
90+ if tile_source .is_block_source :
91+ return tile_source .get_block_details (mirror , tile_coords , self .zoom_level )
9292 return tile_source .get_tile_url (mirror , tile_coords , self .zoom_level )
9393
9494 def create_tiff_metadata (self , n_images ):
@@ -181,8 +181,8 @@ class ImagePart:
181181 """ A part of a 2D image where height and width are not necessarily
182182 of the same size. Provides readout of the defined sub-area of the image.
183183 """
184- def __init__ ( self , path , x_min_src , x_max_src , y_min_src , y_max_src , x_dst , y_dst ):
185- self .path = path
184+ def __init__ ( self , meta , x_min_src , x_max_src , y_min_src , y_max_src , x_dst , y_dst ):
185+ self .meta = meta
186186 self .x_min_src = x_min_src
187187 self .x_max_src = x_max_src
188188 self .y_min_src = y_min_src
@@ -208,19 +208,39 @@ def __str__(self):
208208 f'({ self .x_max_src } , { self .y_min_src } )' )
209209
210210 def get_image (self ):
211- # Open the image
212- try :
213- r = requests .get (self .path , allow_redirects = True , verify = verify_ssl , timeout = 1 )
214- if not r :
215- raise ValueError (f"Could not get { self .path } " )
216- if r .status_code != 200 :
217- raise ValueError (f"Unexpected status code ({ r .status_code } ) for { self .path } " )
218- img_data = r .content
219- bytes_read = len (img_data )
220- except requests .exceptions .RequestException as e :
221- raise ImageRetrievalError (self .path , str (e ))
222-
223- image = PILImage .open (BytesIO (img_data ))
211+ if type (self .meta ) == str :
212+ # Open the image
213+ try :
214+ r = requests .get (self .meta , allow_redirects = True , verify = verify_ssl , timeout = 1 )
215+ if not r :
216+ raise ValueError (f"Could not get { self .meta } " )
217+ if r .status_code != 200 :
218+ raise ValueError (f"Unexpected status code ({ r .status_code } ) for { self .meta } " )
219+ img_data = r .content
220+ bytes_read = len (img_data )
221+ except requests .exceptions .RequestException as e :
222+ raise ImageRetrievalError (self .meta , str (e ))
223+
224+ image = PILImage .open (BytesIO (img_data ))
225+ elif type (self .meta ) == dict :
226+ mirror = self .meta ['mirror' ]
227+ tile_coord = self .meta ['tile_coord' ]
228+ x = tile_coord [0 ] * mirror .tile_width
229+ y = tile_coord [1 ] * mirror .tile_height
230+ z = self .meta ['tile_coord' ][2 ]
231+
232+ cutout = self .meta ['dataset' ][
233+ x :(x + mirror .tile_width ),
234+ y :(y + mirror .tile_height ),
235+ z ]
236+
237+ # FIXME: Don't just assume first channel (last dimension below)
238+ image_data = np .transpose (cutout [:,:,0 ,0 ])
239+ bytes_read = len (image_data )
240+ image = PILImage .frombuffer ('RGBA' ,
241+ (mirror .tile_width , mirror .tile_height ), image_data , 'raw' , 'L' , 0 , 1 )
242+ else :
243+ raise ValueError (f'Unknown image meta data type: { self .meta } ' )
224244
225245 src_width , src_height = image .size
226246
@@ -374,6 +394,11 @@ class BB:
374394 width = 0
375395 height = 0
376396
397+ def __str__ (self ):
398+ return f'Bounding Box: ({ self .px_x_min } , { self .px_y_min } , { self .px_z_min } ) - ' + \
399+ f'({ self .px_x_max } , { self .px_y_max } , { self .px_z_max } ) ' + \
400+ f'[width: { self .width } , height: { self .height } ]'
401+
377402
378403def extract_substack_no_rotation (job ) -> List :
379404 """ Extracts a sub-stack as specified in the passed job without respecting
@@ -484,9 +509,9 @@ def extract_substack_no_rotation(job) -> List:
484509 cur_px_y_max = bb .px_y_max - y * tile_height
485510 # Create an image part definition
486511 z = bb .px_z_min + nz
487- path = job .get_tile_path (stack , mirror , (x , y , z ))
512+ source_meta = job .get_source_metadata (stack , mirror , (x , y , z ))
488513 try :
489- part = ImagePart (path , cur_px_x_min , cur_px_x_max ,
514+ part = ImagePart (source_meta , cur_px_x_min , cur_px_x_max ,
490515 cur_px_y_min , cur_px_y_max , x_dst , y_dst )
491516 image_parts .append ( part )
492517 except Exception as e :
0 commit comments