Loading...
Liberty Point, Sharanpur Road, Nashik. +91 9309079965 info@heuristictechpark.com

Building a Serverless Image Processor with Python and AWS Lambda

Home Blog
Building a Serverless Image Processor with Python and AWS Lambda

Building a Serverless Image Processor with Python and AWS Lambda

In today's visual world, image processing is essential for countless applications. From e-commerce websites showcasing product photos to social media platforms handling user uploads, the ability to manipulate images efficiently is critical. But scaling image processing can be a challenge, especially as demand fluctuates. That's where serverless architecture comes in.

This post will guide you through building a serverless image processing application using Python and AWS Lambda. You'll learn how to leverage the power of Python libraries like Pillow and OpenCV to perform common image transformations, all while taking advantage of the scalability and cost-effectiveness of AWS Lambda. No servers to manage, no scaling headaches – just pure image processing power on demand!

What is Serverless and Why Use It?

Before we dive into the code, let's briefly discuss serverless computing. In a nutshell, serverless allows you to run code without provisioning or managing servers. AWS Lambda is a popular serverless compute service that lets you run functions in response to events.

For image processing, serverless offers several advantages:

  • Scalability: Lambda automatically scales to handle any number of image processing requests.
  • Cost-Effectiveness: You only pay for the compute time you consume. If no images are being processed, you pay nothing.
  • Simplified Management: No servers to patch, update, or monitor. Focus on your code, not infrastructure.

(The Architecture)

Our serverless image processing application will follow this architecture:

  • Image Upload: Users upload images to an AWS S3 bucket.
  • Lambda Trigger: The S3 upload event triggers an AWS Lambda function written in Python.
  • Image Processing: The Lambda function uses libraries like Pillow or OpenCV to process the image (e.g., resize, watermark, convert format).
  • Result Storage: The processed image is stored back in a designated output folder within the S3 bucket.
  • (Step-by-Step Implementation)

    Let's break down the implementation step-by-step:

    1. Set up an AWS Account and IAM Role:

    If you don't have one already, create an AWS account.

    Create an IAM role with permissions to access S3 and execute Lambda functions. Make sure the role includes policies like AWSLambdaExecute and AmazonS3FullAccess (or more restricted policies if possible for better security).

    2. Create an S3 Bucket:

    Create an S3 bucket to store your images. You'll need two folders within the bucket: one for input images (e.g., input/) and one for output images (e.g., output/).

    3. Write the Python Lambda Function:

    Create a new Lambda function in the AWS Lambda console. Choose Python as the runtime.

    Here's an example Python code snippet that resizes an image using the Pillow library:

    "import boto3

    from io import BytesIO

    from PIL import Image

    import os


    s3 = boto3.client('s3')


    def lambda_handler(event, context):

        bucket = event['Records'][0]['s3']['bucket']['name']

        key = event['Records'][0]['s3']['object']['key']

        size = event['Records'][0]['s3']['object']['size']


        try:

            # Download the image from S3

            response = s3.get_object(Bucket=bucket, Key=key)

            image_data = response['Body'].read()


            # Open the image using Pillow

            image = Image.open(BytesIO(image_data))


            # Resize the image

            new_width  = 500

            new_height = int(image.size[1] * (new_width / image.size[0]))

            image = image.resize((new_width, new_height))


            # Save the resized image to a BytesIO object

            output_stream = BytesIO()

            image.convert('RGB').save(output_stream, format="JPEG")

            output_stream.seek(0) # rewind the data


            # Upload the resized image back to S3

            output_key = 'output/' + os.path.basename(key).split('.')[0] + '_resized.jpg' # New file name

            s3.put_object(Bucket=bucket, Key=output_key, Body=output_stream)


            print(f"Resized image uploaded to s3://{bucket}/{output_key}")


            return {

                'statusCode': 200,

                'body': f"Resized image uploaded to s3://{bucket}/{output_key}"

            }


        except Exception as e:

            print(e)

            raise e"

    Explanation:

    • The code downloads the image from S3 using boto3 (the AWS SDK for Python).
    • It opens the image using the Pillow library.
    • It resizes the image to a new width (adjust the new_width variable as needed). The height is calculated to maintain the aspect ratio.
    • It saves the resized image to a BytesIO object (in memory).
    • It uploads the resized image back to S3 to the output folder.

    Important: Make sure you have the Pillow library installed. You'll need to create a deployment package for your Lambda function (more on this below).

    4. Configure the Lambda Function:

    In the AWS Lambda console, configure the following:

    • Runtime: Python 3.x (choose the latest supported version)
    • Handler: lambda_function.lambda_handler (This assumes your Python file is named lambda_function.py and the function is named lambda_handler)
    • Memory: Start with 128MB or 256MB, and adjust as needed based on the size of the images you're processing.
    • Timeout: Increase the timeout to give the function enough time to process larger images (e.g., 30 seconds).
    • Environment Variables (Optional): You can use environment variables to store sensitive information like API keys.

    5. Create a Deployment Package:

    AWS Lambda requires you to upload your code and any dependencies as a deployment package.

    Since we're using Pillow, we need to include it in the package. The easiest way to do this is to create a virtual environment:

    "mkdir lambda_package

    cd lambda_package

    python3 -m venv venv

    source venv/bin/activate  # On Linux/macOS

    # venv\Scripts\activate  # On Windows

    pip install Pillow

    pip install boto3

    pip install --target . Pillow  # Install Pillow directly into the current directory

    zip -r9 ../lambda_function.zip .

    cd ..

    zip -g lambda_function.zip lambda_function.py # If lambda_function.py is outside the directory"

    • This will create a zip file named lambda_function.zip that contains your code and the Pillow library.

    6. Upload the Deployment Package:

    In the AWS Lambda console, upload the lambda_function.zip file.

    7. Configure the S3 Trigger:

    • In the AWS Lambda console, add an S3 trigger to your Lambda function.
    • Configure the trigger to activate when an object is created in your S3 bucket.
    • Specify the input/ folder as the prefix to only trigger the function for images uploaded to the input folder.

    (Testing the Application)

    To test your serverless image processor:

  • Upload an image to the input/ folder in your S3 bucket.
  • Check the output/ folder. After a few seconds, you should see the resized image.
  • Monitor the Lambda function's logs in CloudWatch to see if there were any errors.
  • (Beyond Resizing: Other Image Processing Tasks)

    This example focused on resizing, but you can easily adapt the code to perform other image processing tasks:

    • Watermarking: Add a watermark to the image using Pillow's ImageDraw module.
    • Format Conversion: Convert the image to a different format (e.g., PNG to JPEG).
    • Thumbnail Generation: Create smaller thumbnail versions of the image.
    • Image Enhancement: Adjust brightness, contrast, or sharpness.
    • Object Detection: Use OpenCV to detect objects in the image.

    Final Tech Thoughts:

    Building a serverless image processing application with Python and AWS Lambda is a powerful way to handle image transformations at scale without the hassle of managing servers. By leveraging the scalability and cost-effectiveness of serverless architecture, you can create a robust and efficient solution for a wide range of image processing needs. Experiment with different image processing techniques and explore the possibilities of serverless computing!

    Ready to take your cloud skills to the next level? Explore our Python and AWS training programs at Heuristic Technopark Nashik!

    Contact Us

    +91 9309079965

    info@heuristictechpark.com

    © Copyright Heuristic Technopark All Rights Reserved.