Project Overview: Image Resizing and Padding Application using Flask
This project involves the development of a web application built using Flask, where users can upload images, resize them to specified dimensions, and pad them with a customizable background color. The application provides a simple, user-friendly interface for uploading images, inputting desired dimensions, and selecting a background color. After the user submits the form, the app processes the images by resizing and padding them, and then saves them in a designated folder.
Key Features:
Image Upload:
- Users can select and upload multiple image files (with supported extensions: PNG, JPG, JPEG).
- The files are received through an HTML form using
multipart/form-data
.
Dynamic Resizing and Padding:
- The images are resized to fit within the user-specified dimensions (width and height).
- If the image does not have the same aspect ratio as the target dimensions, it is padded with a background color to ensure the output image matches the required size.
Custom Background Color:
- Users can specify a background color for the padding. The color is provided in hexadecimal format (e.g.,
#ffffff
for white). - The color input is processed and converted from hex to RGB, which is then used to fill the padding area of the resized image.
- Users can specify a background color for the padding. The color is provided in hexadecimal format (e.g.,
Concurrent Image Processing:
- Multiple images are processed in parallel using Python’s
ThreadPoolExecutor
. This speeds up the processing time when dealing with multiple images.
- Multiple images are processed in parallel using Python’s
Output Folder:
- A “Processed” folder is created inside the directory where the images are stored.
- Processed images are saved in JPEG format with a resized and padded version.
User Feedback:
- The app provides feedback to the user with messages indicating whether the processing was successful or if there were errors (such as invalid input or missing files).
- These messages are displayed dynamically on the webpage.
Project Breakdown:
1. Frontend (HTML – select.html
):
The frontend of the application is simple and intuitive, allowing users to:
- Upload multiple image files using the
<input type="file" />
field. - Enter the target width and height for the resized images using
<input type="number" />
. - Choose a background color for padding via an HTML color picker (
<input type="color" />
).
This data is submitted via a POST
request to the Flask backend for processing.
Key HTML Components:
File Upload:
html<input type="file" id="files" name="files" multiple required>
This allows users to select multiple image files from their local system.
Target Dimensions:
html<input type="number" id="width" name="width" required> <input type="number" id="height" name="height" required>
These fields capture the width and height for resizing the images.
Background Color Picker:
html<input type="color" id="bgcolor" name="bgcolor" value="#ffffff">
This field allows the user to select the background color for padding, providing a visual color picker.
Message Display: The HTML template includes a section to display success or error messages based on the processing outcome:
html<div class="message {{ message_type }}"> {{ message }} </div>
2. Backend (Flask – app.py
):
The backend is implemented using Flask and handles:
- Accepting the user-uploaded files.
- Processing the images by resizing and adding padding.
- Saving the processed images to an output folder.
Backend Workflow:
File Validation: The
allowed_file
function ensures that only images with specific extensions (png
,jpg
,jpeg
) are uploaded.Resizing and Padding (Image Processing): The
resize_and_pad
function resizes the image while maintaining its aspect ratio, and adds padding (with a user-specified background color) to fill the target dimensions.The background color is provided in hexadecimal format (e.g.,
#ffffff
for white), which is converted into an RGB tuple (e.g.,(255, 255, 255)
for white).Concurrent Image Processing: The
ThreadPoolExecutor
is used to process each image in parallel, which improves performance when dealing with multiple files.File Saving: After processing, the images are saved as
.jpeg
files in aProcessed
folder within the current directory.
Important Functions:
allowed_file(filename)
: Validates the file extension.resize_and_pad(image, target_width, target_height, background_color)
: Resizes the image and adds padding.process_image(file_path, output_path, target_width, target_height, background_color)
: Processes an individual image (resizes and pads it).select_and_process()
: Handles the GET and POST requests, processes the form input, and calls the image processing functions.
Processing Flow:
- GET Request: When the user visits the page, the form is displayed.
- POST Request: When the form is submitted, the backend:
- Retrieves the images, dimensions, and background color from the form.
- Saves the uploaded files.
- Processes each image in a separate thread.
- Displays a success or error message depending on the result.
How It Works (User Flow):
User Accesses the Application: The user visits the application URL and is presented with the form to upload images and specify dimensions and background color.
User Uploads Files and Specifies Settings:
- The user selects one or more images.
- The user inputs the desired width and height for the output images.
- The user selects a background color using the color picker.
Image Processing:
- Upon submission, the application:
- Validates the input.
- Processes each image by resizing it to the desired dimensions.
- Adds padding in the user-specified background color.
- Upon submission, the application:
Results:
- The processed images are saved in the “Processed” folder.
- A success message with the output folder path is displayed to the user.
Conclusion:
This project provides a practical solution for resizing and padding images with a customizable background color. It uses Flask for the web server and leverages Python’s Pillow
library for image processing. The use of threading ensures that multiple images can be processed concurrently, improving performance for batch image processing. The user interface is designed to be simple and intuitive, providing users with control over the input and output of their image files.