This program allows users to resize an image by providing its file name, a new name for the resized image, and the desired dimensions. The user chooses between JPG or PNG formats and specifies the new width and height. The program then checks if the input image exists, resizes it to the specified dimensions, and saves the resized version with the new name. Error handling is implemented to ensure valid inputs and catch any issues during the resizing process.This version focuses on the actions taken (resize, choose, save) and avoids passive phrasing.
PROGRAM
HERE IS THE FULL PROGRAM TO CREATE IMAGE RESIER
from PIL import Image
import os
def resize_image(image, resized, size):
if not os.path.exists(image):
print(f"Error: The input image '{image}' does not exist.")
return
try:
with Image.open(image) as img:
print(f"Original size: {img.size}")
resized_img = img.resize(size)
resized_img.save(resized)
print(f"Resized image saved to '{resized}' with size {size}")
except Exception as e:
print(f"Error occurred: {e}")
try:
file = input("Enter the image name (without extension): ")
rename = input("Enter the new name for the resized image (without extension): ")
choose = int(input("Choose extension : jpg(1) or png(2) [1/2]: "))
if choose == 1:
image_name = file + '.jpg'
resize_name = rename + '.jpg'
elif choose == 2:
image_name = file + '.png'
resize_name = rename + '.png'
else:
raise ValueError("Invalid choice. Please enter '1' for JPG or '2' for PNG.")
width = int(input("Enter width: "))
height = int(input("Enter height: "))
new_size = (width, height)
resize_image(image_name, resize_name, new_size)
except ValueError as e:
print(f"Error:{e}. Please enter valid numbers for the width, height, and file extension choice.")
EXPLANATION OF THE PROGRAM
1.Importing Required Modules:
Pillow (PIL) is a popular image processing library in Python. Here, we use it to open and resize images.os is used to check whether the image file exists on the system.
from PIL import Image
import os
2.Defining the resize_image()
Function
This function is responsible for resizing the image and saving the resized version.
def resize_image(image, resized, size):