[docs]defremove_black_borders(img:PIL.Image.Image,threshold:int=0,)->tuple[PIL.Image.Image,numpy.typing.NDArray[numpy.bool_]]:"""Remove black borders of CXR. Parameters ---------- img A PIL image. threshold Threshold value from which borders are considered black. Defaults to 0. Returns ------- A PIL image with black borders removed, and the mask used to remove the black borders from the image, that can be subsequently used to process other related image information (e.g. annotation masks). """img_array=numpy.asarray(img)iflen(img_array.shape)==2:# single channelmask=numpy.asarray(img_array)>thresholdreturnPIL.Image.fromarray(img_array[numpy.ix_(mask.any(1),mask.any(0))],),maskiflen(img_array.shape)==3andimg_array.shape[2]==3:r_mask=img_array[:,:,0]>thresholdg_mask=img_array[:,:,1]>thresholdb_mask=img_array[:,:,2]>thresholdmask=r_mask|g_mask|b_maskreturnPIL.Image.fromarray(img_array[numpy.ix_(mask.any(1),mask.any(0))],),maskraiseNotImplementedError