Compress Image
Compress Image
Description
This snippet of code will Compress the image upto a great extent without affecting quality of image.
Requirements
$ pip install Pillow
Steps To Execution
- Fork this repo and navigate to ShortenLinks folder
- (optional) Add images to same directory with this
code.py. - Modify the names in
code.py - Run this code.py
$ python code.py - Boom !!! a new image will generate with much less size but same quality.
- Enjoy the the Image Compress Thing !!!!!
Code Output

| Before | After |
|---|---|
![]() |
![]() |
| Size : 7.3 MB | Size : 1.7 MB |
Source Code: img_comp.py
from PIL import Image
import os
def compress_image():
''' Takes an image file and compress it without losing image quality. '''
''' If no image file is provided, the default image will be compressed '''
try:
image_name = str(input("Enter the name of the image you want to compress: "))
default_image=Image.open(f'{image_name}')
except FileNotFoundError:
default_image=Image.open('original_image.jpg')
# Get image 'width' and 'height'
w, h = default_image.size
# Separate the file name from the extension
default_image_name = os.path.splitext(os.path.basename(default_image.filename))
# compress image
default_image = default_image.resize((w, h), Image.ANTIALIAS)
# return compressed image
return default_image.save('{}_compressed{}'.format(default_image_name[0], default_image_name[1]))
# Run
compress_image()
