-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage_converter.py
30 lines (22 loc) · 1003 Bytes
/
Image_converter.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
from PIL import Image
import os
def convert_image(input_path, output_format):
try:
# Open the image file
img = Image.open(input_path)
# Extract file name without extension
file_name, _ = os.path.splitext(input_path)
# Create the output file name
output_path = f"{file_name}.{output_format.lower()}"
# Save the image in the new format
img.save(output_path, format=output_format.upper())
print(f"Image successfully converted to {output_format.upper()} and saved as {output_path}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
# User input for file path
input_file = input("Enter the image file path (e.g., image.jpg): ").strip()
# User input for output format
output_format = input("Enter the desired output format (e.g., png, jpg, webp): ").strip().lower()
# Convert the image
convert_image(input_file, output_format)