Pil: Fromarray Gives A Wrong Object In P Mode
I want to load an image in P mode, transform it into np.array and then transform it back, but I got a wrong Image object which is a gray image, not a color one label = PIL.Image.op
Solution 1:
Images in 'P' mode require a palette that associates each color index with an actual RGB color. Converting the image to an array loses the palette, you must restore it again.
label = PIL.Image.open(dir).convert('P')
p = label.getpalette()
label = np.asarray(label)
img = PIL.Image.fromarray(label, mode='P')
img.setpalette(p)
img.save('test.png')
Post a Comment for "Pil: Fromarray Gives A Wrong Object In P Mode"