Apply tricolor posterization to images with specified colours.
Colours in colors.txt
shamelessly stolen from (accessed 2024-10-22)
https://webflow.com/blog/best-color-combinations
More examples in examples/
$ pip install -r requirements.txt
$ python3 tricolor.py --help
positional arguments:
images Paths to input image files.
options:
-h, --help show this help message and exit
--color COLOR Hex color code (e.g., 0x1E2761). Specify exactly three times.
--plot If set, generates a plot with the original, tricolor image, and color swatches.
$ python3 tricolor.py test/Lena.jpeg --color 0xFF0000 --color 0xFFFFFF --color 0x0000FF
Saved: Lena_0xFF0000_0xFFFFFF_0x0000FF.png
$ ./test/tricolor_test.sh
Saved: Lego_0x1E2761_0x408EC6_0x7A2048.png
Saved plot: Lego_plot_0x1E2761_0x408EC6_0x7A2048.png
Saved: Lena_0x1E2761_0x408EC6_0x7A2048.png
Saved plot: Lena_plot_0x1E2761_0x408EC6_0x7A2048.png
Saved: Starry_Night_0x1E2761_0x408EC6_0x7A2048.png
Saved plot: Starry_Night_plot_0x1E2761_0x408EC6_0x7A2048.png
...
I don't know much about colour theory, so it has surprised me that there is only one correct way to order the palette (top left in the above picture). I expected the results of different orderings to be slightly off, or even interesting, but no, they look downright wrong and unappealing. We divide the image into dark, mid, and light regions, and likewise always reorder the user-supplied palette from dark to bright.
We use American English (in particular, "color") in code. In comments and documentation, it matters not (I prefer to use British English --rdancer).