The wild broccoli π₯¦
Current mood: feeling
My return from the weekend journey
Mood: π venturesome written under the waxing crescent π
O happy day, I managed to finish the homework that was set out for me in the previous week. Now I have more homework to do! In fact, there’s an essay that I need to write by midnight tonight, so I have less than 6 hours in which to complete it (from the start of me drafting this blog post). I’ve also received the grade for my machine learning homework (88%, so please don’t use my answer from the previous post for your learning at homt) and a new assignment has appeared. I also have to submit a final project proposal by Friday. On top of all that, there is a midterm tomorrow and another midterm next Monday at 9 a.m., so my natural tendency was to…
Go on a fun weekend road trip with my husband and our friend Matt!

It was really lovely, and I’ll have to steal the photos off my husband’s camera once he has put them on the computer π
You might notice that the photo is slightly crunched. This is a stylistic choice! I wrote a little script that uses ImageMagick to dither the photos. The way to implement dithering is pretty simple, and I have a little suspicion that a lot of the web-hosted dithering sites are using ImageMagick under the hood… π§ββοΈ But that’s just my conspiracy theory.
Unfortunately, if you read the documentation, you can’t use ordered dithering with a custom palette in ImageMagick… or can you?
Anyway, here’s how to do it yourself in the command line!
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.jpg +dither -quantize Lab -colors 16 -unique-colors palette.png
3magick photo.jpg \( +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
Line 1 defines the \(\text{M}_4\) Bayer threshold map1, line 2 quantizes your picture into a 16-color palette using the LAB colorspace, and then line 3 perturbs the image with the Bayer filter and then remaps it to the arbitrary palette defined in line 2 (but the matching here is in RGB because I think it looks better).
You can of course change the number of colors, and if you want to change how “strong” the dithering effect is, you can change the args argument to be some variation of \(\left(0,X,1,-\frac{1}{2}X\right)\). These are just the options I liked. A value of 0 will just be regular posterization, while a value of 1 becomes really noisy and shifts everything towards grey (because it is working on every color channel).
If you want to make multiple images follow the same palette, you can append them to each other when defining palette.png in line 2 like so:
1magick *.jpg -resize 256x256! -append +dither -quantize Lab -colors 16 -unique-colors \
2 palette.png
This makes one big image out of all your images and then tries to calculate a palette from that super-image.
-
By the way, the
P2 4 4 15part at the start is not the matrix, but defines the PGM greyscale 4x4 matrix with possible values ranging from 0-15. ↩︎