How to compress image size before saving to the database in Django | image optimization using PILLOW/PIL - Python

Images are the stuff that makes a webpage heavy. One should always try to keep the images optimized for a good page speed index. You may find several different ways on the internet to compress the image size before saving it to the database. Some developers use very complicated ways for solving this issue.
Today I will provide the 2 easiest solutions for this problem.

NOTE: In Python, the name you use to install something e.g. from open-cv, using pip, is not necessarily the same as the name you use to import it.

The pillow is one such case, you use pip install pillow it for installing the package but you use import PIL. That's because Pillow is just a repackaged, updated version of PIL, the original maintainers of the PIL stopped working on it a long time ago. PIL hasn't been updated since 2009.

Method 1

If you are uploading the image using the form, then you can use this method. I am making a function that takes 2 arguments. The first one is the image itself and the second is an integer value i.e. the required quality ( from 1 to 100 ).
You can remove the second argument from the function if you want a default quality value for all images. Just put some constant integer value from 1 to 100 for quality.

Example: Use request.FILES.get('image') as image value/argument if you are uploading an image in a POST request (i.e. using the form). On the other hand, if you are passing the image from the database then use object.image.path.

The .path gives the path of the file.

uploading image in POST request

Method 2

Here is another method to compress the image size automatically when the model's class object is saved. The " save " function as shown in the example below will take the image and override the original image by compressing its size as per your requirement. This method is an alternative to the first one. You can do something like this :

override the original image by compressing its size automatically

Tips

If you are using forms to upload images, then you should use the first method. On the other hand if the number of images are quite big it will be more complex to use the second method.
If you are using the Django admin app to store images then you have no choice i.e. you have to use the second method.

Both of the above methods are easy and tested, you can just copy the code & feel free to make changes.
A feedback to this post will be highly appreciated .

Recent Articles

Changing Database from SQLite to MySQL - Django

One of the main benefit to use django for web development is that it comes with an inbuilt database/filesystem i.e SQLITE. Managing database and files is super easy in django. But there are some drawbacks of using SQLITE...

  - 2023-07-12
How to download a file in django

Sometimes we have to serve a file which may be a large file from our project directory. Suppose you want to transfer a file from your project directory to your friend or to some other person/server/platform....

  - 2023-07-12
How to change base url or domain in sitemap - Django

A sitemap is the first important SEO step which elaborates a website's content to search engines. It provides information to search engines about the available content on our site, which helps them to crawl webpages for indexing....

  - 2023-07-12
How to Make a Website

After reading this article you will never ever search for web development guide neither on Google nor on YouTube....

  - 2023-07-12
What is Javascript - Features and usages

I'm kind of a person who always likes to get started by installing the things & writing some code. I love that but this time JavaScript requires a little bit of the theoretical basic so that we can understand what things we are learning, how hard and powerful it is and where they can be applied....

  - 2023-07-12
Top 5 Interview Questions : Tips for HR round

In this article we are talking about the most famous HR questions and the mind-set to answer those HR questions....

  - 2023-07-12
How to get job in IT - Perfect resume guide

In today's article I want to talk about how to prepare the best resume to get a job in IT. Yes, everybody wants to have a job and in order to get a job you have to apply for it and when you apply for that, the resume is one good thing that you always submit....

  - 2023-07-12


^