The wild broccoli π₯¦
Current mood: feeling
Escape From Midterm Island
Mood: π₯² weary written under the waning gibbous π
It’s a happy Wednesday, because my evil exams are over for now! As my last exam fell right before a break, I don’t know how I did, of course, but at least I was able to answer all the questions on both this midterm and the one the previous Thursday. I will consider this a win, especially since the exam time was quite short and I’m sure that many people, myself included, were trying to come up with solutions up until the last second.
I’ve implemented a silly new feature to my blog:
On a different topic, I realized that the code example I gave last post might be a little confusing for people. It’s basically a way to generalize Bayer dithering with any palette: I add a threshold offset to each pixel and then snap to the nearest color in my palette.
From before:
1echo "P2 4 4 15 0 8 2 10 12 4 14 6 3 11 1 9 15 7 13 5" | magick pgm:- bayer.png
2magick photo.png +dither -quantize Lab -colors 16 -unique-colors palette.png
3magick photo.png \( +clone -fill bayer.png -draw "color 0,0 reset" \) \
4 -compose Mathematics -define compose:args=0,0.2,1,-0.1 -composite \
5 +dither -quantize Lab -remap palette.png dithered.png
Let’s walk through this step by step with the standard “Baboon”1 test image.
Original image
Close-up
Wow, that’s a lot of colors. Let’s reduce them!
If I tried ImageMagick’s built-in ordered dithering algorithm magick photo.png -ordered-dither o4x4,3 ordered.png, I can can get a pretty good result:
Ordered dither
Close-up
But I think I want a more perceptual palette! I’d also like to have any number of colors (the built-in dithering with ox4x4,N divides the R, G, and B channels into \(N\) thresholds which means that you can only get \(N^3\) potential colors each time, though you can also try out ox4x4,N1,N2,N3 for \(N_1\times N_2\times N_3\) max colors, with each channel getting its own count of levels) Let’s try to implement an arbitrary palette generated from the LAB colorspace.
Step 1: Bayer Matrix π
First, I’ll generate a 4x4 Bayer matrix by writing the 16-level greyscale values by hand:
1echo "P2 4 4 15 0 8 2 10 12 4 14 6 3 11 1 9 15 7 13 5" | magick pgm:- bayer.png
| 0 | 8 | 2 | 10 |
| 12 | 4 | 14 | 6 |
| 3 | 11 | 1 | 9 |
| 15 | 7 | 13 | 5 |

Step 2: Palette generation π¨
Now I can generate my palette. I’m using the L*a*b* colorspace, which is based on the opponent model of human vision and is therefore designed to approximate human vision better than RGB of CMYK.
1magick photo.jpg +dither -quantize Lab -colors 16 -unique-colors palette.png
Hm, this is 15 colors and not 16! Well, for some reason that I can’t figure out in the documentation, if I write -colors N, I’ll actually get \(N-1\) colors but only when I use Lab colorspace quantization. Just keep that in mind when choosing the number of colors.
What happens if I forget the dithering step π
1magick photo.png +dither -quantize Lab -remap palette.png plain_remap.png
Posterization with custom palette
Close-up
Oh…! -remap repainted each pixel with the closest color in the palette, but it looks sort of bad, doesn’t it?
So far, all I’ve managed to do is make something that looks like the posterization filter but with arbitrary, slightly more pleasing colors. But just like with posterization, there are these big bands of color without any detail at all, and I feel like the image got flattened a bit. I don’t like how this looks, so let’s try to fix this.
Step 3: Tile the pattern across the whole picture and combine βΉοΈ
1\( +clone -fill bayer.png -draw "color 0,0 reset" \)
I’ll make a copy of the photo (+clone) and then fill it with the 4x4 pattern I generated before (-fill bayer.png -draw "color 0,0 reset). But now I just have an image of nothing but the grid of thresholds!
Thankfully this isn’t my final image. I’m going to put these two images together now.
1-compose Mathematics -define compose:args=0,0.2,1,-0.1 -composite
My texture
Perturbed image
It looks pretty similar to the original image, but with a little grain to it. This grain is how the magic of dithering will create the Bayer pattern later.
Mathematics lets me write my own formula to blend the two images. The four numbers are \(A, B, C, D\).
ImageMagick is computing this for each pixel and channel:
\[ \text{new pixel} = (A \times\text{photo}\times\text{pattern}) + (B\times\text{pattern}) + (C\times\text{photo}) + D \]With my specified values (I want to keep \(D = \frac{1}{2}B\) and \(A = 0\) so that the brightness stays the same),
\[ \text{new pixel} = \text{photo} + (0.2 \times \text{pattern}) - 0.1 \]Feel free to play around with the values to your heart’s content and experiment!
Step 4: Snap to the palette π«°
OK, now let’s repaint!
1+dither -quantize Lab -remap palette.png plain_remap.png
Pixels sitting near the border between two palette colours get pushed over the border by the pattern, some of them one way and some the other. The result is a checkerboard mix of the two colours, which your eye will blend into an in-between shade.
Final result
Close-up
Yay! We did it! It’s super fast too, since we’re actually combining all this stuff into just three lines.
Bonus: If you like Floyd-Steinberg more
Floyd-Steinberg is a lot easier to implement with an arbitrary palette than ordered dithering. Basically you just have to generate the palette and then point your command at it:
1magick photo.png -dither FloydSteinberg -quantize Lab -remap palette.png floyd.png
This creates a more organic-looking pattern, but at the cost of small pixel differences changing the pattern every time because we’re using error diffusion so what the algorithm does at one location influences what happens at other locations.
Floyd-Steinberg
Close-up
I hope this was fun and useful! Feel free to try this out and change the settings to your own taste. You can also generate a palette from one image and apply it to another one, create a loop that dithers all the images in a folder for you, use a different color space, or try other diffusion patterns altogether! This also works with any image format, not just png. And you can save a lot of bandwidth and space too. I like to resize my images to be less than 720px wide before I run the dithering algorithm, since my site’s article container is only 720px wide at a maximum. But you can really do anything you want π
-
Actually, it is a mandrill! ↩︎