If you don't like NetPBM, you can do it very simply in Python with Pillow as follows:
#!/usr/bin/env python3 from PIL import Image import numpy as np # Load input image im = Image.open('start.png') # Get palette p = im.getpalette() #print(f'{p=}') # Make new palette, by swapping first and last RGB entries firstEntry = p[:3] print(f'{firstEntry=}') lastEntry = p[-3:] print(f'{lastEntry=}') newPalette = [ *lastEntry, *p[3:765], *firstEntry ] print(f'{len(newPalette)=}') #print(f'{newPalette=}') # Make image into Numpy array of palette indices na = np.array(im) # Find all locations with palette entry 0, and all locations with palette entry 255 mask0 = na==0 mask255 = na==255 # Swap 0 for 255 and 255 for 0 na[mask0] = 255 na[mask255] = 0 # Revert to PIL Image and push in new palette newIm = Image.fromarray(na) newIm.putpalette(newPalette) # Save result as PCX newIm.save('result.pcx') Note I have not coded the general case, just the demonstration of swapping palette indices 0 and 255. The other case is not much harder and you can certainly code it from this example. I didn't want to get bogged down in the general case because Python may be a non-starter for you.