_io.py 675 B

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