Median Blur is adding new colors to the image. Any way to avoid this?

that is precisely what I meant by nearest-neighbor lookup. that means finding the palette entry that is the best match for whatever pixel.

that isn’t done with a “switch” statement.

you can do that with numpy… it’s just a bunch of reshaping, broadcasting, argmin, … give it a try. I haven’t done this in a long time.