How can palette indices be reordered without altering displayed colors?
remap_palette builds a palette in the requested order and remaps existing pixel indices accordingly.
Pixel experiment and interpretation
Swap red and blue palette slots and compare the index with the rendered RGB color. Identical appearance does not guarantee identical training IDs.
from PIL import Image
im=Image.new('P',(1,1),1)
im.putpalette([255,0,0,0,0,255])
out=im.remap_palette([1,0])
(out.getpixel((0,0)),out.convert('RGB').getpixel((0,0)))Result (0, (0, 0, 255))
中文
重排调色板时如何同时保留显示外观?
remap_palette 根据目标顺序重建颜色表,并相应重映射已有像素索引。
像素实验与适用边界
将红蓝色槽互换,比较索引与转成 RGB 后的结果。外观一致并不能证明训练标签编号仍一致。
from PIL import Image
im=Image.new('P',(1,1),1)
im.putpalette([255,0,0,0,0,255])
out=im.remap_palette([1,0])
(out.getpixel((0,0)),out.convert('RGB').getpixel((0,0)))结果 (0, (0, 0, 255))