The wild broccoli πŸ₯¦

Current mood: feeling The current mood of oleracea at www.imood.com

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. At the very least I won’t lose points for an empty answer!

I’ve implemented a silly new feature to my blog: red and blue text, that I will try to use the same way as they do in Umineko. Of course, there isn’t a mystery you’re trying to solve, but I think it could be funny to put my theories and true assertions in a specific format… I also decided to include the purple and gold truths but I don’t think I’ll really have much use for them, plus they are a little hard to read on light mode with the original game colors. Maybe if I start hosting quotes from Umineko on here πŸ™‚


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 Original image

Zoomed in 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:

Default ordered Ordered dither

Close-up of 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

Bayer matrix

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

15-color palette 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

Posterized mess Posterization with custom palette

Zoomed in 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

Tiled map My texture

Composed image 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.

Ordered dithering with Lab quantized palette Final result

Zoomed in 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 dithering Floyd-Steinberg

Zoomed in 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 πŸ™‚


  1. Actually, it is a mandrill! ↩︎

#Blog #Code