TL;DR Integrating PIL/Pillow with Flask enables effortless image processing within web applications. By following this guide, you can resize, blur, and rotate images using Python Imaging Library (PIL), now known as Pillow.
Flask Image Processing with PIL/Pillow Integration: A Practical Guide
As a Full Stack Developer, you've likely encountered projects that require image processing capabilities. In this article, we'll delve into integrating the popular Python Imaging Library (PIL), now known as Pillow, with Flask – a lightweight and versatile web framework. By the end of this guide, you'll be able to effortlessly process images within your Flask application.
Why PIL/Pillow?
Before we dive in, let's briefly discuss why PIL/Pillow is an excellent choice for image processing tasks:
- Cross-platform compatibility: PIL/Pillow supports a wide range of platforms, including Windows, macOS, and Linux.
- Extensive library support: It comes with numerous built-in filters, effects, and transformations to help you manipulate images efficiently.
Setting up the Project
To begin, create a new Flask project using your preferred IDE or command-line interface. Install Pillow using pip:
pip install pillow
Next, ensure that Flask is installed as well (if not already):
pip install flask
Create a basic Flask application structure:
# app.py
from flask import Flask, request, jsonify
from PIL import Image, ImageEnhance, ImageFilter
app = Flask(__name__)
@app.route('/process_image', methods=['POST'])
def process_image():
# TO DO: implement image processing logic here
pass
if __name__ == '__main__':
app.run(debug=True)
Processing Images with PIL/Pillow
In this section, we'll explore various image processing techniques using PIL/Pillow. For demonstration purposes, assume you're handling an incoming request containing an image file.
Image Resize
from PIL import Image
@app.route('/process_image', methods=['POST'])
def process_image():
img = request.files['image']
img.save('temp.jpg') # save the image to a temporary file
with Image.open('temp.jpg') as pil_img:
resized_img = pil_img.resize((800, 600)) # resize the image
resized_img.save('resized_image.jpg')
return jsonify({'message': 'Image processed successfully!'})
Image Blur
from PIL import ImageFilter
@app.route('/process_image', methods=['POST'])
def process_image():
img = request.files['image']
img.save('temp.jpg')
with Image.open('temp.jpg') as pil_img:
blurred_img = pil_img.filter(ImageFilter.GaussianBlur(radius=5)) # apply a blur effect
blurred_img.save('blurred_image.jpg')
return jsonify({'message': 'Image processed successfully!'})
Image Rotate
from PIL import Image, ImageOps
@app.route('/process_image', methods=['POST'])
def process_image():
img = request.files['image']
img.save('temp.jpg')
with Image.open('temp.jpg') as pil_img:
rotated_img = ImageOps.mirror(pil_img) # rotate the image
rotated_img.save('rotated_image.jpg')
return jsonify({'message': 'Image processed successfully!'})
Conclusion
Integrating PIL/Pillow with Flask enables you to effortlessly process images within your web application. By following this guide, you've learned how to resize, blur, and rotate images using the popular Python Imaging Library.
Feel free to experiment with other image processing techniques and explore the extensive capabilities of Pillow!
