Working with Image Filters
Transcript
By Arnar Tumi Ăžorsteinsson. From Wagtail Space NL 2022. Published June 30, 2022.
Watch the video Markdown version 501 words · auto-generated · en
Summary
Django’s image filter system can register custom operations through the `register_image_operations` hook in `hooks.py`. Each operation receives a Pillow image and returns a transformed Pillow image, and filters can be chained from the image tag—for example, converting an image to grayscale, then flipping or mirroring it. More practical uses include watermarks, color multiplication, and masking with a grayscale watercolor image; the speaker also showed parameterizing a filter with a hexadecimal color and mentioned ongoing documentation and work to reduce the required boilerplate.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 Image Filter Overview
So basically I was just gonna because there has been quite a lot of discussion about these image filters this week. So I just want to show you a little bit about well, basically what's possible. When we were doing this we found this well basically undocumented hook called register image operations where you can register filter operations in your Loctile hooks pie. And every one of those is basically a pillow image so it's it's a it's a willow
0:48 Registering Image Operations
operation where you where you have a function that takes in a pillow image and basically returns another pillow image after you've done something with it. Then you have this filter operation with a construct and a run that's basically runs this function. So I had a little fun and made a couple of filters Just so I could show you how this works. So we have like grayscale and we have flip and mirror and a couple of things. And all you have to do is basically use the current image tag and you can then just add stuff to the back of it.
1:34 Chaining Built-In Filters
So here we have an image basically Yeah, just a normal image of some landscape. And we want a grayscale version. So then you can just add grayscale here and you have a grayscale version. And you can because of how these work you can just day chain them so you can just add more if you want. So you want to take You know a grayscale one that's flipped and mirrored. Then you have something that's mil I mean many of these filters are not really usable.
2:20 Parameterized Image Effects
I mean they don't make sense in most use cases, but there are cases where you can use them and It's things like watermarks. It's things like multiply, for us at least. So for this kind I want to try to like add variables so you can actually just do multiply and then add any hex variable that you want so multiply this with a gray color. Then you get like a grayish background thing. If you wanna go crazy, you can go with completely red And you can so you can just play around with this. And just
3:06 Masking and Documentation
to kinda end this, I did try to do a little bit of masking as well. So I have a image that is basically a grayscale image of a watercolor. mask so you can then you can just add watercolor and then you'll get something that's completely different just by masking out things like this. Kuhn was doing some documentation on this. So hopefully we'll have some documentation on this register. image filter and there was also some work done on creating a hook that kind of bypasses all this all this boilerplate
So yeah, hopefully someone can use some of this.