A few weeks ago, we received a new ticket: “Users want to be able to apply filters to their pictures”, yes, something like Instagram does. We immediately thought about the Lightroom API, but after some research, we came to the conclusion that it doesn’t quite meet our needs. We were searching for something like a library, where we can just load the image, apply the filters and then save the new image with the filters applied, since we couldn’t find it we decided to build one.
FImage
FImage is a Python module to apply and create multiple filters to images, it exposes an API that you can use for applying the different color transformations to the images. It works by converting the image to an RGB matrix and applying different math formulas to it. We used NumPy for all the matrix operations since it is faster and optimized, and Pillow for handling the loading and saving of the images.
How to use it?
First, we need to install it, for this you need to be using Python 3.6 or greater to be able to use FImage.
pip install fimage
And for these examples, I’m gonna use this picture to apply it filters:
Applying a simple filter
Create a file app.py
with:
from fimage import FImage
from fimage.filters import Sepia
def main():
# replace 'my_picture.jpg' with the path to your image
image = FImage('my_picture.jpg')
# apply the Sepia filter to the image
image.apply(Sepia(90))
# save the image with the applied filter
image.save('my_picture_sepia.jpg')
if __name__ == "__main__":
main()
Now, just run it :
python app.py
And this is how the new image my_picture_sepia.jpg
looks like after the filter was applied.
Applying multiple filters
FImage offers more filters besides the Sepia one, even you can combine multiples filters to give a better look to your picture.
Modify the file app.py
to import more filters from FImage
from fimage import FImage
from fimage.filters import Contrast, Brightness, Saturation
def main():
image = FImage('my_picture.jpg')
# apply the mutiple filters to the image
image.apply(
Saturation(20),
Contrast(25),
Brightness(15)
)
# save the image with the applied filter
image.save('my_picture_mixed.jpg')
if __name__ == "__main__":
main()
We run it by
python app.py
And our new my_picture_mixed.jpg
looks like
The order in which the filters are passed to the apply
function matters, this is because the filters are applied sequentially, so the next filter will be applied over the resultant image from the previous one.
Presets
Presets are just the combinations of multiple filters with already defined adjustment values.
Let’s change our app.py
one more time to use the Presets
from fimage import FImage
from fimage.presets import SinCity
def main():
# replace 'my_picture.jpg' with the path to your image
image = FImage('my_picture.jpg')
# apply the SinCity preset to the image
image.apply(SinCity())
# save the image with the applied preset
image.save('my_picture_sincity.jpg')
if __name__ == "__main__":
main()
After we run it, we get our new my_picture_sincity.jpg
Custom Presets
If you like the look your picture got after testing different filters and want to store this combination for applying it to more pictures, you can create your own Preset by just extending the Preset
Class and specifying these filters and their adjust values in it.
In our app.py
let’s do
from fimage import FImage
from fimage.presets import Preset
from fimage.filters import Contrast, Brightness, Saturation
# Create my custom preset and specify the filters to apply
class MyOwnPreset(Preset):
transformations = [
Contrast(30),
Saturation(50),
Brightness(10),
]
def main():
# replace 'my_picture.jpg' with the path to your image
image = FImage('my_picture.jpg')
# apply MyOwnPreset to the image
image.apply(MyOwnPreset())
# save the image with the applied preset
image.save('my_picture_custom.jpg')
if __name__ == "__main__":
main()
The new my_picture_custom.jpg
This is basic usage of FImage, we are still developing it, and it would be really great any feedback or contribution you have.
You can follow me on Twitter and GitHub to be up to date with all my projects and content.
Top comments (7)
What advantages does your library provide over imagemagick?
How does it compare speed-wise?
FImage is a pure Python library and it allows you to create and even modify some of the filters ( I'm not sure if you can do this with ImageMagick).
About the speed, I will need to do some tests, in average FImage lasts around one second applying a filter, this depends on the size of the image as well.
If you're using numPy, you're already using c libraries and not Pure Python. Equivalent to using one of the python based shims built around libmagic.
Awesome, Thanks
Awesome article!
hey, sorry I'm late as hell to this. But I'm making a software project that uses fimage and I cannot get it to install using pip, I've tried lots of different methods, anyone know how to get it installed to my python library?
hello
how can I convert an image edit by one of these filters to a cv2 format ?
thank you