A script that compresses and makes diferent sizes of all images in child directories
This script processes images. For each image, it creates several versions including thumbnails, lazy loading and diferent resolutions. It is intended to use for web or app applications, where all the images are on the server, and the user gets only the one that suits their resolution screen.
It navigates through children folders and compresses all images in them. Creates a new folder called compressed
with all the new compressions in the same folder names of the original images.
SQ-S
: 50x50SQ-M
: 200x200SQ-L
: 400x400LZ-SQ
: 50x50 lazy load image
The resolution for each version indicates the smaller side of the image (width for portrait and height for landscape). It is thought like that following the 720p, 1080p... notation.
The other dimension of the image is smaller_side*aspect_ratio
and would depend on the original image itself.
FULL-XS
: 480p, ~SDFULL-S
: 720p, ~HDFULL-M
: 1080p, ~FullHDFULL-L
: 1280p, ~WUXGAFULL-XL
: 1440p, ~2k/QHDFULL-XXL
: 2160p, ~4k/UHDLZ-FULL
: 200p
The script uses a value of 80% for the normal images and a 10% for the lazy load images. The lazy load images are intended to be blurred when being displayed to the user. The compression value affects the actual JPG compression (where 100% would not make any compression at all).
Also, for the non lazy-load images, a slight sharpness process is applied in order to enhance the image after the resizing.
Regular resizing algorightms may not match the pixels of the original image completely, that is, they might put 1081 pixel input image in a 1080 pixel output image. That causes an important loss of quality, because the pixels do not fit. This algorithm takes into account the aspect ratio, and if the pixels do not fit, it crops by 1 pixel the image, in order to not loose quality. A maximum row or column of 1 pixel can be cropped, because sometimes min_size*aspect_ratio
may not be integer. Note that a single pixel crop is not visible to human sight and can be a better option in front of loosing quality.
The orginal images can be found at images folder.
![](https://raw.githubusercontent.com/pujaltedavid/python-image-compressor/main/example.png)
Simply copy the script in the parent folder that has children folders with images to be compressed.
By default, it would make a central cropping for the square images. To crop any image differently, just add -option-
to the source filename. Below are the options for cropping an horizontal or vertical image.
![](https://raw.githubusercontent.com/pujaltedavid/python-image-compressor/main/options.png)
1
: slight left | top2
: total left | higher3
: slight right | lower4
: total right | bottom
To apply this changes, rename the file to be compressed as <filename.jpg> -> <filename-option-.jpg>
Left crop example: DSC_1000.JPG -> DSC_1000-2-.JPG.
For a central cropping just leave the file name as it is.
This repository is created and maintained by David Pujalte.