Everything you need as a full stack developer

Flask Image Processing with PIL/Pillow integration

- Posted in Flask by

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!

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more