64base image to pil image and upload s3 bucket on aws lambda

This article is about how to convert base64 string image to byte and invoke to PIL image.
Then we can handle image whatever we want to ex) image resize ..

base64 image come from img_base64 = event['base64Image']
and then convert to byte data imgdata = base64.b64decode(img_base64)

then it can save to image file and load to Image.open(filename)
invoke to pil image directly : img2 = Image.open(io.BytesIO(imgdata))

In the source code, there is also example how to upload image to s3 bucket.
refer to below code.
import sys

import json
import boto3
import os
import io
from PIL import Image
import base64

ACCESS_KEY = os.environ.get('ACCESS_KEY')
SECRET_KEY = os.environ.get('SECRET_KEY')

def uploadToS3(bucket, s3_path, local_path):
    s3_client = boto3.client(
    s3_client.upload_file(local_path, bucket, s3_path)
def lambda_handler(event, context):
    #base64 image string
    img_base64 = event['base64Image']
    #string to byte
    imgdata = base64.b64decode(img_base64)
    #save some image file
    with open("/tmp/imageToSave.png", "wb") as fh:
    uploadToS3("input-image1", "imageToSave.png", "/tmp/imageToSave.png")
    #load image file to pil
    img = Image.open("/tmp/imageToSave.png")
    width, height = img.size
    img2 = Image.open(io.BytesIO(imgdata))
    width2, height2 = img2.size
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello leon it is from Lambda!'),
        'width file': width,
        'heihgt file' : height,
        'width pil': width2,
        'heihgt pil' : height2


These method is referenced by:

Thanks for effort.


python zip test sample code

numberList = [1, 2, 3, 4, 5]
strList = ['one', 'two', 'three', 'five']

# No iterables are passed
result = zip()
print('first zip: {}'.format(result))

# Two iterables are passed
result = zip(numberList, strList)
print('input value to zip: {}'.format(result))

# Converting itertor to list
print('zip to list: {}'.format(list(result)))

# Converting itertor to set
resultSet = set(result)
print('zip to set: {}'.format(resultSet))



How to install Node.js and npm on Amazon Linux (CentOS 7) for lambda

npm & node version check
> node --version
> npm --version

install or upgrade npm & node
Change setup_8.x, in case you want to install version 8.x
>curl -sL https://rpm.nodesource.com/setup_10.x | bash -
>yum install nodejs

If you already installed old version npm & node, remove first.
>yum remove -y nodejs npm

I have installed node as 8.x.



Install python 3.6 on AmazonLinux and make Docker image

Run Amazon Linux Container (and tunnelling)
> docker run -v $(pwd):/outputs --name lambdapack -d amazonlinux:latest tail -f /dev/null

Run container bash shell
> docker exec -i -t lambdapack /bin/bash
Install python 3.6
bash# yum -y update
bash# yum -y upgrade
bash# yum install -y \
 wget \
 gcc \
 gcc-c++ \
 findutils \
 zlib-devel \

bash# yum install -y yum-utils
bash# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
bash# yum install -y python36.x86_64
bash# yum install -y python36-devel.x86_64
bash# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
bash# python36 get-pip.py
bash# pip3 install virtualenv
bash# exit

Make docker Image
> docker commit lambdapack marearts/amazon_linux_py36:v1.0.0

Push Docker Image to Docker Hub
>docker login
>docker push marearts/amazon_linux_py36:v1.0.0

Take Care!