May 3, 2020, 12:45 a.m.
How to compress image size before saving to database in Django | image optimization using PILLOW/PIL - Python
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.


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
How to deploy a django website on pythonanywhere | django Web app deploying...
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 ...
View all...
Popular Posts
How to compress image before saving to DBMigrating from SQLite to MySQL in Django
Choosing correct M.2 SSD
How to deploy a web app on pythonanywhere
5 Steps to create sitemap in Django
How to send mails automatically in Django
Handling arguments in django urls
Media and Static configuration in Django
Language Translation & detection using python
React Js vs Angular Js
5 Tips to improve Programming Logics