OSError: cannot write mode F as PNG When try use array_to_data_url function for thresholding the CT images


I try to use the below code in Dash to convert the NumPy array of CT images into an image data string.

img_slices, seg_slices = [
        # top
        [dash_canvas.utils.array_to_data_url(im[i, :, :]) for i in range(im.shape[0])],
        # side
        [dash_canvas.utils.array_to_data_url(im[:, i, :]) for i in range(im.shape[1])],
    for im in [ct_img, seg_img]

But it gives me the above error.

I am attempting to apply the Hounsfield unit thresholding for the CT images. Is there any method that can I apply thresholding for all the slices at once rather than considering only the middle slice?

Your suggestions are highly appreciated. I am stuck here for weeks.

Thank You