-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image Compresser.py
42 lines (34 loc) · 1.25 KB
/
Image Compresser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from PIL import Image
import os
def compress_jpeg(input_file, output_file, quality=100):
with Image.open(input_file) as img:
img = img.convert("RGB") # Ensure image is in RGB mode for JPEG
img.save(output_file, 'JPEG', quality=quality)
def get_file_size(file_path):
return os.path.getsize(file_path)
# Get input from the user
input_file = input("Enter the path of the image file: ").strip()
quality = input("Enter the Quality (1-100): ").strip()
# Validate and convert quality to integer
try:
quality = int(quality)
if not (1 <= quality <= 100):
raise ValueError("Quality must be between 1 and 100.")
except ValueError as e:
print(f"Invalid quality value: {e}")
exit(1)
compressed_file = 'compressed_image.jpg'
# Compress the image
try:
compress_jpeg(input_file, compressed_file, quality=quality)
except Exception as e:
print(f"Error compressing image: {e}")
exit(1)
# Check file sizes
try:
original_size = get_file_size(input_file)
compressed_size = get_file_size(compressed_file)
print(f'Original file size: {original_size} bytes')
print(f'Compressed file size: {compressed_size} bytes')
except Exception as e:
print(f"Error checking file sizes: {e}")