May 3, 2020, 12:45 a.m.

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

Images are the stuff which makes a webpage heavy . One should always try to keep the images optimized for a good pagespeed index. You may find several different ways on internet to compress the image size before saving to database. Some developers uses very complicated ways for solving this issue.
Today i will provide 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.

Pillow is one such case, you use pip install pillow for installing 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 updated since 2009

Method 1 If you are uploading the image using form, then you can use this method. I am making a function which takes 2 arguments .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 image in POST request (i.e using form) . On the other hand if you are passing the image from database then use object.image.path .

The .path gives path of the file.

image not found
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 for the first one. You can do something like this :
image not found
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 django admin app to store images then you have no choice i.e you have to use 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 .







Some recent posts



Complete process of changing database from SQLite to MySQL - How to migrat...

" How to download a file in django | Making a large file downloadable from ...

How to use proxy in python requests | http & https proxies - Scraping...

Top Django Querysets, How to filter records | Fetching data from database e...

How to change base url or domain in sitemap - Django ...

How to Make a Website - Everything you should know about web development...

What is Javascript - Features and usages ...

Top 5 Interview Questions : Tips for HR round ...

How to get job in IT : Perfect resume guide for IT job ...

Programming vs coding | difference between programming and coding ...



View all...