xuebinqin / DIS

This is the repo for our new project Highly Accurate Dichotomous Image Segmentation
Apache License 2.0
2.22k stars 258 forks source link

Not able to save image: ValueError: Can't write images with one color channel. #87

Open wern44 opened 1 year ago

wern44 commented 1 year ago

Hello, I hope someone is able to help me here as I am completely stuck. I get the follwoing error when running the inference: ValueError: Can't write images with one color channel. -> triggered on line 53 when saving the image.

I already tried different things (cmap="Gray") etc. but nothing worked.

Thank you for any ideas!

Trace: Traceback (most recent call last): File "main\IS-Net\inference.py", line 53, in io.imsave(os.path.join(result_path,im_name+".png"),(result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8)) File "R:\OneDrive\Kiart\Batch\Python\DIS_AI\DIS-main\venv\lib\site-packages\skimage\io_io.py", line 143, in imsave return call_plugin('imsave', fname, arr, plugin=plugin, *plugin_args) File "main\venv\lib\site-packages\skimage\io\manage_plugins.py", line 205, in call_plugin return func(args, kwargs) File "main\venv\lib\site-packages\imageio\v3.py", line 147, in imwrite encoded = img_file.write(image, kwargs) File "main\venv\lib\site-packages\imageio\plugins\pillow.py", line 405, in write raise ValueError("Can't write images with one color channel.") ValueError: Can't write images with one color channel.

CzakuGim commented 1 year ago

Quick fix: Use:

import cv2
cv2.imwrite(os.path.join(result_path,im_name+".png"),(result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8))

Instead of:

io.imsave(os.path.join(result_path,im_name+".png"),(result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8))
wern44 commented 1 year ago

Thank you for your help! It worked perfectly.