@@ -52,6 +52,15 @@ def create_parser():
5252 default = BLOCK_LEN ,
5353 )
5454
55+ parser .add_argument (
56+ "--pad" ,
57+ help = "Automatically pad image files at the bottom and right borders. "
58+ "Use this, when the input images don't have a common size, but have "
59+ "their origin at (0, 0)." ,
60+ default = False ,
61+ action = "store_true" ,
62+ )
63+
5564 add_verbose_flag (parser )
5665 add_distribution_flags (parser )
5766
@@ -89,6 +98,7 @@ def cubing_job(
8998 batch_size ,
9099 image_size ,
91100 num_channels ,
101+ pad = False ,
92102):
93103 if len (z_batches ) == 0 :
94104 return
@@ -109,13 +119,32 @@ def cubing_job(
109119 for z , file_name in zip (z_batch , source_file_batch ):
110120 # Image shape will be (x, y, channel_count, z=1) or (x, y, z=1)
111121 image = read_image_file (file_name , target_wkw_info .dtype )
112- assert (
113- image .shape [0 :2 ] == image_size
114- ), "Section z={} has the wrong dimensions: {} (expected {})." .format (
115- z , image .shape , image_size
116- )
122+ if not pad :
123+ assert (
124+ image .shape [0 :2 ] == image_size
125+ ), "Section z={} has the wrong dimensions: {} (expected {}). Consider using --pad." .format (
126+ z , image .shape , image_size
127+ )
117128 buffer .append (image )
118129
130+ if pad :
131+ x_max = max (slice .shape [0 ] for slice in buffer )
132+ y_max = max (slice .shape [1 ] for slice in buffer )
133+
134+ buffer = [
135+ np .pad (
136+ slice ,
137+ mode = "constant" ,
138+ pad_width = [
139+ (0 , x_max - slice .shape [0 ]),
140+ (0 , y_max - slice .shape [1 ]),
141+ (0 , 0 ),
142+ (0 , 0 ),
143+ ],
144+ )
145+ for slice in buffer
146+ ]
147+
119148 # Write batch buffer which will have shape (x, y, channel_count, z)
120149 # since we concat along the last axis (z)
121150 buffer = np .concatenate (buffer , axis = - 1 )
@@ -175,6 +204,7 @@ def cubing(source_path, target_path, layer_name, dtype, batch_size, args=None) -
175204 batch_size ,
176205 (num_x , num_y ),
177206 num_channels ,
207+ args .pad ,
178208 )
179209 )
180210
0 commit comments