[PYTHON] Create an image composition app with Flask + Pillow

Create an image compositing application using Flask. Install the Pillow module.

Overview

Select the image to be combined from the form, and select the file you want to combine, such as the logo. A composite image is displayed in the execution result. This time, the following two images are combined.

limited_car.jpg

momo.png

As usual, it's a peach again.

Code reality

The name will be overwritten at the time of compositing, but please do something about it.

imgadd.py


import os
from PIL import Image
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, session
from werkzeug import secure_filename
app = Flask(__name__)

UPLOAD_FOLDER = './uploads'
ALLOWED_EXTENSIONS = set(['jpg','png','gif'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SECRET_KEY'] = os.urandom(24)

def allowed_file(filename):
    return '.' in filename and \
        filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/')
def index():
    return render_template('img.html')


@app.route('/show_img', methods=['GET', 'POST'])
def show_img():
    if request.method == 'POST':
        img_data = request.files['img_data']
        logo_data = request.files['logo_data']

        if img_data and allowed_file(img_data.filename):
            filename = secure_filename(img_data.filename)
            img_data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            SQUARE_FIT_SIZE = 600
            MAIN_FILENAME = 'uploads/' + filename
            im = Image.open(MAIN_FILENAME)
            im.thumbnail((SQUARE_FIT_SIZE, SQUARE_FIT_SIZE))
            width, height = im.size

        if logo_data and allowed_file(logo_data.filename):
            filename = secure_filename(logo_data.filename)
            logo_data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            SQUARE_FIT_SIZE = 100
            LOGO_FILENAME = 'uploads/' + filename
            logo_im = Image.open(LOGO_FILENAME)
            logo_width, logo_height = logo_im.size

            im.paste(logo_im, (width-logo_width, height-logo_height), logo_im)
            im.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            img_url = '/uploads/' + filename

            return render_template('img.html', img_url=img_url)


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

if __name__ == '__main__':
    app.debug = True
    app.run()

img.html


{% extends "base.html" %}
{% block content %}
<form method="post" action="{{ url_for('show_img') }}" enctype="multipart/form-data">
Select the main image<input type="file" id="img_data" name="img_data">
Select logo<input type="file" id="logo_data" name="logo_data">
  <input type="submit" value="Send">
</fomr>
<div>
  {% if img_url %}
  <p><img src="{{ img_url }}"></p>
  {% endif %}
</div>
{% endblock %}

Run

スクリーンショット 2017-06-06 12.58.13.png

It's a simple form, but I'll send you an image right away.

スクリーンショット 2017-06-06 12.58.46.png

Successful.

Recommended Posts

Create an image composition app with Flask + Pillow
Create an image processing viewer with PySimpleGUI
Create a simple web app with flask
[Python] Quickly create an API with Flask
Create an English word app with python
Create an app that guesses students with python
Create an image with characters in python (Japanese)
Create an animated GIF local server with Python + Flask
Create an app that guesses students with python-GUI version
Image processing with PIL (Pillow)
Create an environment with virtualenv
Create an API with Django
Create Image Viewer with Tkinter
Let's create an app that authenticates with OIDC with Azure AD
Create a Todo app with Django ① Build an environment with Docker
Cut out an image with python
Run the app with Flask + Heroku
Create an Excel file with Python3
Creating a simple app with flask
Create an age group with pandas
Image upload function with Vue.js + Flask
Create an application by classifying with Pygame
Azure table storage with PTVS Flask app
Deploy flask app with mod_wsgi (using pipenv)
Create a dummy image with Python + PIL.
Quickly create an excel file with Python #python
Create a GUI app with Python's Tkinter
Create an update screen with Django Updateview
Easy web app with Python + Flask + Heroku
Create your first app with Django startproject
Validate each pixel value when resizing an image with Pillow with nearest neighbor
I tried playing with the image with Pillow
Easy image processing in Python with Pillow
Create an add-in-enabled Excel instance with xlwings
[Golang] Create docker image with Github Actions
Create a web service with Docker + Flask
Getting Started with Heroku, Deploying Flask App
Fastly replace image colors with PIL / Pillow
Try to generate an image with aliasing
Create an upgradeable msi file with cx_Freeze
[kotlin] Create an app that recognizes photos taken with a camera on android
Create an app that works well with people's reports using the COTOHA API
Create an academic society program with combinatorial optimization
Create polka dot wallpaper with Python Image Library
Vienna with Python + Flask web app on Jenkins
How to crop an image with Python + OpenCV
Create a bulletin board with Heroku, Flask, SQLAlchemy
How to create a multi-platform app with kivy
Post an article with an image to WordPress with Python
Create an API server quickly with Python + Falcon
Let's make an app that can search similar images with Python and Flask Part1
Build an image classification model explosively with Azure Custom Vision and implement it with Flask
I want to convert an image to WebP with lollipop
Create a Todo app with Django REST Framework + Angular
Create a native GUI app with Py2app and Tkinter
Create an image file using PIL (Python Imaging Library).
Easy machine learning with scikit-learn and flask ✕ Web app
Create a Todo app with the Django REST framework
Create an idol-like tweet with Keras LSTM (sentence generation)
An easy way to create an import module with jupyter
Create a Todo app with Django ③ Create a task list page