@@ -133,9 +133,18 @@ def save_masked_image(seg_dir, image_dir, output_dir, fname):
133133 # resize the segmentation to match the image, so smaller segmentations
134134 # can be used to mask larger images (for example in localisation stages)
135135 if im .shape [:2 ] != seg .shape [:2 ]:
136- seg = resize (seg , (im .shape [0 ], im .shape [1 ], 3 ), order = 0 )
137- im [:][seg [:, :, 2 ] == 0 ] = 0 # make background black.
138- imsave (os .path .join (output_dir , im_fname ), im , quality = 95 )
136+ seg = resize (seg , (im .shape [0 ], im .shape [1 ]),
137+ order = 0 , preserve_range = True ,
138+ anti_aliasing = False ).astype (seg .dtype )
139+ im [seg == 0 ] = 0 # make background black
140+ out_path = os .path .join (output_dir , im_fname )
141+ ext = os .path .splitext (im_fname )[1 ].lower ()
142+ if ext in [".jpg" , ".jpeg" ]:
143+ Image .fromarray (im ).save (out_path , quality = 95 , subsampling = 0 , optimize = True )
144+ else :
145+ imsave (out_path , im )
146+
147+
139148
140149def save_corrected_segmentation (annot_fpath , seg_dir , output_dir ):
141150 """assign the annotations (corrections) to the segmentations. This is useful
@@ -207,4 +216,10 @@ def gen_composite(annot_dir, photo_dir, comp_dir, fname, ext='.jpg'):
207216 with warnings .catch_warnings ():
208217 # avoid low constrast warning.
209218 warnings .simplefilter ("ignore" )
210- imsave (out_path , comp , quality = 95 )
219+ ext = os .path .splitext (out_path )[1 ].lower ()
220+ if ext in [".jpg" , ".jpeg" ]:
221+ Image .fromarray (comp ).save (out_path , quality = 95 , subsampling = 0 , optimize = True )
222+ else :
223+ imsave (out_path , comp )
224+
225+
0 commit comments