Choosing the tolerance
The tolerance is a distance in RGB space: the straight-line difference between a pixel's colour and the picked one, from 0 (identical) to 441 (black to white). Flat graphics need little more than 0 for their solid areas; their anti-aliased edges are blends towards the neighbouring colour, so with soft edges on, a tolerance just under the distance to that neighbour recolours the edges in proportion and leaves no fringe. Photos and JPEGs need a larger value, because noise and compression spread every colour out. Watch the “Changed” share: if it jumps, the tolerance has started to catch other colours.
What the script handles for you
- Colour profiles: photos in Display P3 (most phone and Mac photos) are converted to sRGB first, so the colour you picked on screen is the colour the script looks for.
- Phone photos stored sideways are turned upright with
ImageOps.exif_transpose(). - 16-bit images are scaled to 8 bits instead of being clipped to white.
- Transparency: fully transparent pixels are left alone, and the output is PNG whenever the image has or gains transparency; photos without it stay JPEG.
How the scripts are tested
The build generates the script for every combination of action, tolerance and soft edges on a transparent logo, a photo, a sideways phone photo, a 16-bit PNG and a transparent GIF, runs each with Pillow 13.0.0 and NumPy, and checks that the picked colour is really gone from the logo and that phone photos come out upright. The method, and four others timed against it, is explained in counting and replacing colours.