Introduction: 

Image compression is a vital process for websites and applications, as it can significantly reduce the file size of images while maintaining acceptable quality. This reduction helps improve website load times, reduce bandwidth usage, and optimize storage space. One useful tool for compressing images is the Py-Image-Compressor, a command-line Python utility developed by George Khananaev. In this article, we will guide you through the process of installing and using the Py-Image-Compressor tool to compress your images efficiently.

Prerequisites

Before we start, ensure that you have the following software installed on your system:

  1. Python 3.6 or higher: Download and install the appropriate version of Python for your operating system from the official Python website (https://www.python.org/downloads/).

  2. Git (optional): If you would like to clone the repository from GitHub, install Git from the official Git website (https://git-scm.com/downloads).

Installation

To install the Py-Image-Compressor tool, follow these steps:

  1. Open your terminal or command prompt.

  2. Clone the Py-Image-Compressor repository using the following command:

				
					git clone https://github.com/georgekhananaev/py-image-compressor.git

				
			

Alternatively, you can download the repository as a ZIP file from the GitHub page and extract it to a folder on your system.

  1. Navigate to the py-image-compressor directory:
				
					cd py-image-compressor

				
			
  1. Install the required Python packages using the following command:
 
				
					pip install -r requirements.txt

				
			

The Py-Image-Compressor tool is now ready to use.

Usage

To compress images with the Py-Image-Compressor tool, follow these steps:

  1. Place the images you want to compress in the input folder located inside the py-image-compressor directory.

  2. Run the following command to compress the images:

				
					python main.py -l <Your Location> -d <Your Destination> -f <File Format> -w <Max Width> -q <Max Quality> -r <Remove Larger Files>

				
			
CommandWeightDefault ValuesMeaningDetailsUsage example
-lrequiredN/Alocationyour images path, where your original images located.-l "C:/Original Images/"
-doptional/data/output/destinationyour destination path, compressed images will be saved here-d "C:/Compressed Images/"
-woptional1920max widthif larger resolution will be set to max width without breaking the image ratio-w 1920
-qoptional80qualityimages quality by percentage.
lower quality to save more space
-q 80
-foptionaljpegformatsupported format “.png”, “.jpeg”, “.jpg”, “.ppm”, “.gif”, “.tiff”, “.bmp”, “.webp”, “.heic”, “.heif”
for more check PIL Documentation.
-f jpeg
-roptionalnoremoveremove images from destination path,
if compression is worst than original file.
-r y
-hhelpif you forgot what command you want to use can write python main.py –help-h or --

Example:

				
					python main.py -l "D:/React/resume-website/" -d "./data/out/" -f webp -w 500 -q 100
				
			
Compress a React Website demo using multi cores with Py-Image-Compressor.

Found this article interesting? Check out my GitHub: https://github.com/georgekhananaev

Social Share: