-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
image_tools.py
74 lines (59 loc) · 2.6 KB
/
image_tools.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from PIL import Image
import os
def resize_image(input_path, output_path, new_width, new_height):
"""Resize the image to the specified dimensions."""
try:
with Image.open(input_path) as img:
resized_img = img.resize((new_width, new_height))
resized_img.save(output_path)
print(f"Image resized and saved to {output_path}")
except Exception as e:
print(f"Error resizing image: {e}")
def convert_image_format(input_path, output_path, new_format):
"""Convert the image to a different format."""
try:
with Image.open(input_path) as img:
img.save(output_path, format=new_format.upper())
print(f"Image converted to {new_format} and saved to {output_path}")
except Exception as e:
print(f"Error converting image format: {e}")
def rotate_image(input_path, output_path, degrees):
"""Rotate the image by the specified number of degrees."""
try:
with Image.open(input_path) as img:
rotated_img = img.rotate(degrees, expand=True)
rotated_img.save(output_path)
print(f"Image rotated by {degrees} degrees and saved to {output_path}")
except Exception as e:
print(f"Error rotating image: {e}")
def main():
while True:
print("\nImage Tools Menu:")
print("1. Resize Image")
print("2. Convert Image Format")
print("3. Rotate Image")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == '4':
print("Exiting the program.")
break
if choice not in ['1', '2', '3']:
print("Invalid choice, please choose a valid option.")
continue
input_path = input("Enter the path to the input image: ")
if not os.path.exists(input_path):
print("The specified file does not exist.")
continue
output_path = input("Enter the path for the output image: ")
if choice == '1':
width = int(input("Enter the new width: "))
height = int(input("Enter the new height: "))
resize_image(input_path, output_path, width, height)
elif choice == '2':
new_format = input("Enter the new image format (e.g., JPEG, PNG): ")
convert_image_format(input_path, output_path, new_format)
elif choice == '3':
degrees = float(input("Enter the number of degrees to rotate the image: "))
rotate_image(input_path, output_path, degrees)
if __name__ == "__main__":
main()