-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimizer.py
57 lines (45 loc) · 2.22 KB
/
optimizer.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import argparse, sys, os
from PIL import Image, UnidentifiedImageError
class ImageOptimizerError(Exception):
pass
def image_optimizer(input_file, output_file, quality=85, scale=0.8, verbose=True):
"""
Compress and resize an image.
:param input_file: Path to the original image
:param output_file: Path to save the output file
:param quality: Compression quality (0-100 | default: 85)
:param scale: Scale of resizing (default: 80%)
:param verbose: Whether to print messages (default: True)
"""
input_ext = os.path.splitext(input_file)[1].lower()
output_ext = os.path.splitext(output_file)[1].lower()
if input_ext != output_ext:
raise ImageOptimizerError(f"Input file type '{input_ext}' does not match output file type '{output_ext}'.")
try:
imagem = Image.open(input_file)
except FileNotFoundError:
raise ImageOptimizerError(f"File '{input_file}' was not found.")
except UnidentifiedImageError:
raise ImageOptimizerError(f"File '{input_file}' is not a valid image.")
width, height = imagem.size
new_width = int(width * scale)
new_height = int(height * scale)
imagem = imagem.resize((new_width, new_height), Image.ANTIALIAS)
imagem.save(output_file, optimize=True, quality=quality)
if verbose:
print(f"Image successfully optimized and save as '{output_file}'.")
def main():
parser = argparse.ArgumentParser(description="Compress and resize images.")
parser.add_argument("input_file", type=str, help="Path to the original image")
parser.add_argument("output_file", type=str, help="Path to save the output file")
parser.add_argument("--quality", type=int, default=85, help="Compression quality (0-100)")
parser.add_argument("--scale", type=float, default=0.8, help="Scale of resizing (0.1 a 1.0)")
parser.add_argument("--verbose", action=argparse.BooleanOptionalAction, default=True, help="Whether to print messages (--verbose / --no-verbose)")
args = parser.parse_args()
try:
image_optimizer(args.input_file, args.output_file, args.quality, args.scale, args.verbose)
except ImageOptimizerError as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()