Create image resizer

INTRODUCTION

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):
3.checking if the image exixts

    The os.path.exists() function checks whether the image file specified by the user exists. If the file doesn’t exist, an error message is displayed, and the function exits early.

if not os.path.exists(image):
    print(f"Error: The input image '{image}' does not exist.")
    return

4.Opening,Resizing the Image and saving the resized image

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}")
5.Error handling

This block catches any unexpected errors that might occur during the image processing and prints an appropriate error message

except Exception as e:
    print(f"Error occurred: {e}")

6.Choosing the file extension(JPG or PNG)

        The user chooses whether the image is a JPG or PNG by entering 1 for JPG or 2 for PNG.Based on the user’s choice, the image file names are constructed with the appropriate extension. If an invalid choice is made, a ValueError is raised.

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.")
7.Getting  new  dimensions for resizing
The user is prompted to enter the width and height for the resized image.These values are stored in a tuple, new_size, which is passed to the resize_image() function
width = int(input("Enter width: "))
height = int(input("Enter height: "))
new_size = (width, height)

output

 

 

CONCLUSION

This program efficiently resizes images based on user input using the Pillow library. By prompting users to select file names, image formats, and dimensions, it provides a flexible way to manipulate image sizes. The inclusion of error handling ensures the program operates smoothly, addressing common issues like missing files or invalid inputs. Overall, this program is a simple yet practical tool for basic image resizing tasks, demonstrating the power of Python for image processing with minimal c

 

4o

 

window.__oai_logHTML?window.__oai_logHTML():window.__oai_SSR_HTML=window.__oai_SSR_HTML||Date.now();requestAnimationFrame((function(){window.__oai_logTTI?window.__oai_logTTI():window.__oai_SSR_TTI=window.__oai_SSR_TTI||Date.now()}))

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top