Dynamic image resizing with Python and Serverless Framework
Tags: serverless, python, s3, tutorial
Published: 2022-05-09 15:08:55

Originally posted at [Serverless](https://www.serverless.com/blog/dynamic-image-resizing-python/) on June 27th, 2019

![](https://cdn-images-1.medium.com/max/5200/0*-wcGMS3O2wqfl1Cv.png)

Images are essential to creating an engaging user experience, but more isn’t always better. Large, high-resolution images may require little effort to integrate into the user interface, but they can drag down download speeds for the whole webpage — and the impact of all that additional points of resolution on the user experience is often minimal.

Imagine that you run a news site. A large percentage of your readers are viewing your site on their phones and don’t need the high-resolution pictures. Some of your users, though, are reading on a desktop computer where they have a better connection and a better screen, and so will appreciate the higher-quality images.

The right option from the user experience standpoint is to provide all your images in different sizes based on the device that you are visiting from. However, new devices with unexpected screen proportions come online unpredictably, and resizing an image in advance to fit any conceivable screen size is virtually impossible. Should we pay a huge storage bill and pre-generate all images in all possible sizes? Should we generate each image on the fly for every request? Neither option sounds like a good idea.

However, what if we created an image in each size when it’s first requested, and then saved it for later? This way each device would get the right image size for it, and we would save significantly on the storage costs and the compute costs.

This also is a use case that Serverless is a great fit for. With Serverless, you only use the compute you need at each moment, and you only pay for the compute you use. The Serverless applications are already designed to auto-scale to suit the user demands, so you don’t need to pre-scale any servers and can, therefore, reduce your costs even further. Even where the resizing of images in our use case is necessary, the compute costs are much lower when the resizing is done by a Serverless function.

## Using S3 and Python to scale images with Serverless

In this article, we use Python within the [Serverless framework](https://serverless.com/framework/) to build a system for automated image resizing. We use S3, the AWS cloud storage service, as an example, but Serverless also works well with other cloud services including GCP and Azure.

Storing images in S3 is an easy, scalable way to avoid the high compute costs of hosting a vast library of pre-scaled images without sacrificing the versatility of a dynamic image interface. Essentially, what we’ll do in this example is let each request generate the image of the size it needs. We then store the result in our S3 bucket.

Next time someone requests the same image, one of two things will happen: if the image already exists in that size, the corresponding S3 URI will serve us the previously stored image directly. But if we don’t have the image in that size yet, following the S3 link will first generate the image in that size and then get it served to us, as well as saving that newly resized image in the cloud for future use.

This is what we mean by a “smart” resizing system: instead of preparing for every possible outcome of an image request, we let the users request the images in the sizes they actually need.

## Creating a Serverless API for image resizing

How exactly do we go about implementing the Serverless image-resizing API? In this section, we’ll cover the following steps:

* Writing a serverless.yml config file that contains all the specifics for the resizing service

* Implementing the resizing logic in a resizing function

* Setting up an S3 bucket to work with the resizing endpoint

Our plan is to have our S3 bucket do most of the work: if the image is present in the size we want in the S3 bucket, we just need to serve it to the customer. If the requested size of the image is not available in S3 yet, however, we will have our S3 bucket call our image resizing function which will create the image in the size that we need and respond to the end user with the resized image.

Let’s start by looking at the serverless.yml that sets up everything that will be needed for our function.

First, we specify the name of our service, its runtime and location, and grant permissions to our future function to access S3:
{% gist https://gist.github.com/richardgrantserverless/ffd022aa5b93c9e3cf723e640b865223.js %}
Note that the Resource declaration in the iamRoleStatements policy includes my bucket name. You'll need to change it for your bucket name.

Next comes the definition of the function that we are going to expose and its parameters:
{% gist https://gist.github.com/richardgrantserverless/6c9ecfd71334daea32939fb4fae9190c.js %}
We also need a Resource declaration for the S3 bucket where we will store all the images:
{% gist https://gist.github.com/richardgrantserverless/859651ed9c272c5b9fbbb8c72e1f2d6e.js %}
That’s it for our serverless.yml file. You can view the full version of it [here](https://github.com/chief-wizard/serverless-python-image-resizing/blob/master/serverless.yml).

Now, let’s look at the implementation of the image resizing function in Python. We start by importing a number of modules that we will need in the function:
{% gist https://gist.github.com/richardgrantserverless/85dadf2a510769c56e149475be18fb4a.js %}
The json and datetime modules are self-explanatory. boto is the Python wrapper for the Amazon Web Services API which we will need to download and upload images from and to S3. We use Pillow for image resizing - that's what PIL is. We also include BytesIO as our function will work with file streams, and the os module so that we get access to the environment variables via os.environ.

Let’s now take a look at the very outer function in our file:
{% gist https://gist.github.com/richardgrantserverless/8ad6c141a12bb2c0a573fc2a4df664ae.js %}
This is the function that will get called when there is a new incoming request for an image to be resized. We parse out the key and the size properties from the named path elements that we put down in the serverless.yml file previously. We then call resize_image with the key to the image and the new size that we need. Finally, we return the 301 redirect to the result_url location where our new resized image is.

Let’s see what the resize_image function does under the hood. First, it gets the image from S3 and reads it into a variable:
{% gist https://gist.github.com/richardgrantserverless/d54851593283b9c3e6f09ed6113009f3.js %}
Second, it resizes the image it just read to the new size:
{% gist https://gist.github.com/richardgrantserverless/305847690c6d2ba0be2088ec1a73bf9d.js %}
Third, it uploads the newly resized image back to S3:
{% gist https://gist.github.com/richardgrantserverless/719c9b3ffe552972cc586854d1a3351a.js %}
And the final step, it returns the resized image URL so that we can put it into the 301 redirect in the outer function:
{% gist https://gist.github.com/richardgrantserverless/d0ccdee3a22a58cbc20d108ff5c6b0ca.js %}
The resized image URL is built in a separate function as follows:
{% gist https://gist.github.com/richardgrantserverless/eb86a9c363a981ebfd1b670c5a6082d0.js %}
At this point, we have the outer function call that resizes an image and performs the 301 redirect to the new location, which is the business logic we wanted for the function. You can see the entire handler.py file [right here](https://github.com/chief-wizard/serverless-python-image-resizing/blob/master/handler.py) on GitHub.

Our requirements.txt file for our function is just one line, as we only need two dependencies:
{% gist https://gist.github.com/richardgrantserverless/e0d3e58f3a2842ead7313d39f59e3b99.js %}
We now have everything we need for our image resizing function and can proceed to deploy it.

## Deploying the Serverless API for image resizing

In order to deploy our function, we need the API credentials to our AWS account with permissions to access AWS Lambda, S3, IAM, and API Gateway. To configure these credentials, we use a .env file. You can find an example .env file in the example repo on GitHub [here](https://github.com/chief-wizard/serverless-python-image-resizing/blob/master/.env.example). Please make sure that you never check the .env file into Git, as that might leak your AWS credentials.

To make sure that our Python dependencies compile correctly both in development and in production, we use [the serverless-python-requirements plugin](https://github.com/chief-wizard/serverless-python-image-resizing/blob/master/.env.example). It will make sure that, independently of the operating system we are developing on, the Python dependencies will get packaged correctly for the Lambda environment (Amazon Linux). If developing on a non-Linux system, deploying our function will require Docker to be installed and running.

So in order to deploy our function we need to:

* Fill out the credentials in the .env file.

* Run sls deploy.

There is one last step before everything is functional. In the output of the deploy we get the URL of the function on the AWS API Gateway, it looks like this:

    [https://XXXXX.execute-api.eu-west-1.amazonaws.com](https://XXXXX.execute-api.eu-west-1.amazonaws.com)

We now need to configure our S3 bucket to work together with our Serverless function as follows:

* Configure our S3 bucket for website hosting as shown in the [S3 documentation](https://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html).

* In the [Advanced Conditional Redirects](https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html#advanced-conditional-redirects) section of the Website Hosting settings for the S3 bucket, set up the following redirect rule:
{% gist https://gist.github.com/richardgrantserverless/08de355b01af6fb0f134ed1c38474000.js %}
In place of XXXXX we add the Lambda endpoint of our Serverless function from the deployment step output.

## Seeing the API in action

Once we configure the redirect rule on S3, we have a fully working solution.

![](https://cdn-images-1.medium.com/max/2850/0*na8vb7iaDGGyPgj-.gif)

Once we upload the image to the S3 bucket, we can get the function to resize the image to the size we need on the fly.

## Wrapping Up

In this article, we walked you through setting up a dynamic image resizing API with Python and the Serverless framework.

Image resizing is a great use case for Serverless. When implemented with Serverless, the resizing of the images scales effectively with the load. The function will only use the compute it needs to quickly resize the images — you won’t waste compute time if there are no resizing requests. The solution with S3 and a Serverless function also provides for a very simple architecture and minimizes the number of moving parts, therefore ensuring the stability of the system.

There are many other use cases that can benefit from Serverless, from workflow automation and event streaming to backends for mobile apps and log processing.

If you’d like to explore Serverless, start with the [Serverless documentation](https://serverless.com/framework/docs/getting-started/) and check out [the Introduction to AWS with the Serverless framework](https://serverless.com/framework/docs/providers/aws/guide/intro/). If you are not using AWS you can find the documentation for your provider on the [Providers page](https://serverless.com/framework/docs/#Supported-Providers).

You can find the full example project in this [GitHub repository](https://github.com/chief-wizard/serverless-python-image-resizing).

[![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zvexinkcq4p0m3c5mq9.png)](https://console.serverless.com/?view=register&utm_campaign=Console%20Signup&utm_source=dev.to&utm_medium=post&utm_content=Dynamic%20image%20resizing%20with%20Python%20and%20Serverless%20Framework)

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/dynamic-image-resizing-python/).*