Harnessing Flask for Image Processing in Python
In the dynamic world of web development, Python's Flask framework stands out for its simplicity and flexibility. When it comes to handling images, Flask offers a range of possibilities. Let's delve into how Flask can be used for image processing, making your web applications more interactive and visually appealing.
Understanding Flask and Image Processing
Flask, a micro web framework written in Python, is perfect for small applications and APIs. For image processing, we'll use the Pillow library, a fork of the Python Imaging Library (PIL). Pillow provides a wide range of image processing features, making it an excellent choice for our needs.
Installing Flask and Pillow
Before we start, ensure Flask and Pillow are installed in your Python environment. You can install them using pip:

pip install flask pillow
Setting Up a Basic Flask Application
Let's set up a simple Flask application to handle image uploads and processing.
Creating the Flask Application
Create a new Python file (e.g., `app.py`) and import the necessary modules:
from flask import Flask, request, send_from_directory
from PIL import Image
import os
Initialize Flask and configure the application:

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
Uploading Images
Create a route to handle image uploads:
@app.route('/upload', methods=['POST'])
def upload_image():
if 'file' not in request.files:
return 'No file part'
file = request.files['file']
if file.filename == '':
return 'No selected file'
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
return 'File uploaded successfully'
Processing Images with Flask and Pillow
Now, let's create a function to process images using Pillow. For this example, we'll resize images to a maximum width of 300 pixels.
Resizing Images
Add the following function to `app.py`:

def resize_image(image_path, output_path):
img = Image.open(image_path)
max_width = 300
width_percent = (max_width / float(img.size[0]))
new_height = int((float(img.size[1]) * float(width_percent)))
img = img.resize((max_width, new_height), Image.LANCZOS)
img.save(output_path)
Then, create a route to trigger the image resizing:
@app.route('/resize/')
def resize(filename):
input_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
output_path = os.path.join('static/', filename)
resize_image(input_path, output_path)
return send_from_directory('static', filename)
Displaying Processed Images
Create a simple HTML template (e.g., `index.html`) to display the processed images:
<!DOCTYPE html>
<html>
<head>
<title>Flask Image Processing</title>
</head>
<body>
<img src="{{ url_for('resize', filename=filename) }}">
</body>
</html>
And update `app.py` to render this template:
from flask import render_template
@app.route('/')
def index():
return render_template('index.html', filename='example.jpg')
Running the Application
Finally, run the Flask application using the following code:
if __name__ == '__main__':
app.run(debug=True)
Now, when you navigate to `http://127.0.0.1:5000/` in your browser, you should see the processed image. To test the upload functionality, you can use tools like Postman or curl to send a POST request to `http://127.0.0.1:5000/upload` with an image file.
This article has demonstrated how Flask can be used for image processing, from uploading to resizing images. By leveraging Flask's simplicity and Pillow's extensive features, you can integrate image processing into your web applications with ease.



















