Python how to decrease image size keeping quality

If you need to optimize your site you could be interested in decreasing the image size on your site. If you have thousands of images than you will want to use automated/scripted solution.

In this post:

  • Install Image Pil
    • Error ImportError: No module named Image for PyCharm
    • Uninstall Pil/Pillow
  • Decrease image keeping quality with Pil
  • Decrease images by script and Pil

Install Image Pil

In this post we will use module Pil/Pillow to decrease the image size and keep the same quality of the images(or at least from user point of view). In order to use Pil/Pillow you need to install it by:

pip install pil

for python 3 you may need to use:

pip3 install pil

Sometimes admin privileges can be needed so:

sudo pip install pil

You may need to upgrade your pip:

sudo pip3 install --upgrade pip

and then install the pil.

Error ImportError: No module named Image for PyCharm

This is common error for Pil using Ubuntu 18 and Linux Mint 18 with PyCharm. You need to investigate the problem and to use appropriate solution for it.

  1. Check what version of Python you have setup as SDK:
  • Files
  • Project Structure
  • Project Settings
  • Modules
  • Python
  • Python Interpreter

Check Python SDK home path

You should have something like:

/home/user/IdeaProjects/tools/venv/bin/python
  1. Install pil in virtual environment of PyCharm

In this case you are using virtual environment and you need to install your pil as follow:

/home/user/IdeaProjects/tools/venv/bin/pip3 install --upgrade pip
/home/user/IdeaProjects/tools/venv/bin/pip3 install pillow
  1. If you use another installation of python you can check the version of your terminal by:
python --version
which python

depending on the output you can install pil on the correct python installation.

This should solve the error:

ImportError: No module named PIL

Uninstall Pil/Pillow

pip uninstall PIL

alternative installation for pil

You can isntall Pillow which is a PIL fork.

easy_install Pillow
pip install Pillow

or in some cases for Linux Mint 17 or Ubuntu 16 you may run:

sudo apt-get install python-imaging

Decrease image keeping quality with Pil

If you want to decrease the image size of a single picture you can do it with this code example:

from PIL import Image

file = Image.open('../images/1.jpg')
file.size
file.save("../images/2.jpg",quality=25)

As the image 1.jpg is the input and 2.jpg is the output which are stored in another folder named images.

With this quality parameter - 25 you keep the quality and save about 4 times of the storage size:

  • before - 855.9 kB
  • after - 216.5 kB

Depending on the image this rate could be different. From my experiments the average images from pixabay are shrink up to 5 times without noticeable difference in quality.

Decrease images by script and Pil

You can setup a script executed by cron which to check for newly uploaded images and then to invoke script which is decreasing the uploaded images and send them to another folder:

  1. Set up a cron job in Ubuntu/Linux Mint:
crontab -e

add line:

@reboot  /home/user/scripts/decreaseImage.sh
  1. Create new script file:

    /home/user/scripts/decreaseImage.sh

with content:

#!/bin/bash
DIR="/home/user/imagesBig"
inotifywait -m -e move -e create --format '%w%f' "$DIR" | while read f

do
    python /home/user/scripts/PilDecreaseImage.py "$f" "$DIR"
done

This script will call python script:

  1. Create python file:

    /home/user/scripts/PilDecreaseImage.py

with this python code inside (which is getting the newly uploaded images, shrink them and save the new image in folder: /home/user/images/)

#!/usr/bin/python

import sys
import time
from PIL import Image

time.sleep(5) # optional delay for 5 seconds - in case of many images
file = Image.open(sys.argv[1])
file.size

filename = sys.argv[1].replace(sys.argv[2],"")
print ('filename', filename)
file.save("/home/user/images/"+filename,quality=25)

Related Article