-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_image.py
More file actions
45 lines (34 loc) · 1.45 KB
/
Copy pathresize_image.py
File metadata and controls
45 lines (34 loc) · 1.45 KB
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
from PIL import Image
import os
def resize_image(input_path, output_path, width, height, output_format):
try:
image = Image.open(input_path)
resized_image = image.resize((width, height))
resized_image.save(output_path, output_format)
print(f"\nImage resized and saved to {output_path}\n")
except FileNotFoundError:
print(f"\nError: Input file '{input_path}' not found.\n")
except Exception as e:
print(f"\nError: {str(e)}\n")
def main():
input_path = input("\nEnter image name to resize: ")
if not os.path.exists(input_path):
print("\nCan't find the specified file path\n")
return
try:
width = int(input('\nEnter the new width: '))
height = int(input('Enter the new height: '))
if width <= 0 or height <= 0:
print("\nInvalid input. Width and height must be positive integers.\n")
return
extension = input('\nEnter the output format (e.g., PNG, JPG, JPEG): ').lower()
if not extension:
extension = 'png'
output_directory = os.path.dirname(input_path)
output_filename = f'new_{width}x{height}.{extension}'
output_path = os.path.join(output_directory, output_filename)
resize_image(input_path, output_path, width, height, extension)
except ValueError:
print("\nInvalid input. Please enter valid integers for width and height.\n")
if __name__ == "__main__":
main()