_io.py 719 B

1234567891011121314151617181920212223242526
  1. # MIT License
  2. # Copyright (c) Kentaro Wada
  3. import os.path as osp
  4. import numpy as np
  5. import PIL.Image
  6. def lblsave(filename, lbl):
  7. import imgviz
  8. if osp.splitext(filename)[1] != ".png":
  9. filename += ".png"
  10. # Assume label ranses [-1, 254] for int32,
  11. # and [0, 255] for uint8 as VOC.
  12. if lbl.min() >= -1 and lbl.max() < 255:
  13. lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode="P")
  14. colormap = imgviz.label_colormap()
  15. lbl_pil.putpalette(colormap.flatten())
  16. lbl_pil.save(filename)
  17. else:
  18. raise ValueError(
  19. "[%s] Cannot save the pixel-wise class label as PNG. "
  20. "Please consider using the .npy format." % filename
  21. )