Optimization of images with FastAPI

In this example we will see how to upload images and use background tasks to generate resolutions for different
devices

Dependencies

pip3 install FastAPI uvicorn Pillow

Background Tasks

You can define background tasks to be…

In this example we will see how to upload images and use background tasks to generate resolutions for different
devices



Dependencies

pip3 install FastAPI uvicorn Pillow



Background Tasks

You can define background tasks to be run after returning a response.

from fastapi import FastAPI, UploadFile, File, BackgroundTasks
from fastapi.responses import JSONResponse
from os import getcwd
from PIL import Image

app = FastAPI()

PATH_FILES = getcwd() + "/"


# RESIZE IMAGES FOR DIFFERENT DEVICES

def resize_image(filename: str):
    sizes = [{
        "width": 1280,
        "height": 720
    }, {
        "width": 640,
        "height": 480
    }]

    for size in sizes:
        size_defined = size['width'], size['height']

        image = Image.open(PATH_FILES + filename, mode="r")
        image.thumbnail(size_defined)
        image.save(PATH_FILES + str(size['height']) + "_" + filename)
    print("success")


@app.post("/upload/file")
async def upload_file(background_tasks: BackgroundTasks, file: UploadFile = File(...)):

    # SAVE FILE ORIGINAL
    with open(PATH_FILES + file.filename, "wb") as myfile:
        content = await file.read()
        myfile.write(content)
        myfile.close()

    # RESIZE IMAGES
    background_tasks.add_task(resize_image, filename=file.filename)
    return JSONResponse(content={"message": "success"})



Output

dogs.jpg # Original Image

720_dogs.jpg # Resolution 1280 x 720

480_dogs.jpg # Resolution 640 x 480

Example in Github Gist

https://gist.github.com/nelsoncode019/35910eff9c09ca015cfb4748be345133


Print Share Comment Cite Upload Translate Updates
APA

Nelson Adonis Hernandez | Sciencx (2021-11-19T22:32:39+00:00) Optimization of images with FastAPI. Retrieved from https://www.scien.cx/2021/11/19/optimization-of-images-with-fastapi/

MLA
" » Optimization of images with FastAPI." Nelson Adonis Hernandez | Sciencx - Friday November 19, 2021, https://www.scien.cx/2021/11/19/optimization-of-images-with-fastapi/
HARVARD
Nelson Adonis Hernandez | Sciencx Friday November 19, 2021 » Optimization of images with FastAPI., viewed ,<https://www.scien.cx/2021/11/19/optimization-of-images-with-fastapi/>
VANCOUVER
Nelson Adonis Hernandez | Sciencx - » Optimization of images with FastAPI. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/19/optimization-of-images-with-fastapi/
CHICAGO
" » Optimization of images with FastAPI." Nelson Adonis Hernandez | Sciencx - Accessed . https://www.scien.cx/2021/11/19/optimization-of-images-with-fastapi/
IEEE
" » Optimization of images with FastAPI." Nelson Adonis Hernandez | Sciencx [Online]. Available: https://www.scien.cx/2021/11/19/optimization-of-images-with-fastapi/. [Accessed: ]
rf:citation
» Optimization of images with FastAPI | Nelson Adonis Hernandez | Sciencx | https://www.scien.cx/2021/11/19/optimization-of-images-with-fastapi/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.