10/12/2023 0 Comments Pil image resizeYou can also create a Lambda Layer and upload these into that, but that’s up to you to do. These files will be packaged when I call cloudformation package on the Lambda function. p install -upgrade install -r src/api/requirements.txt -t "Unzipping -o libs/PIL.zip -d _build/ # <- Unzips the libs Then I extract it as the last step of the dependency installation. I created a zip file of the required libraries (it’s only Pillow for the moment), and placed it inside a libs folder. I took the easiest way: copying them into the build folder before packaging the code. There are many ways you can inject these libraries into your Lambda function. PIL libraries extracted from the Docker container How to package? Then all the dependencies of the Lambda function will be copied into the ~/test/ folder and you can do whatever you want with them. Note: The path should reflect the correct Python version you defined in the Dockerfile. docker cp :/var/lang/lib/python3.8/site-packages/ ~/test/ Then run the below command (in a new terminal) to copy the files from the container into your computer. Note down the container ID of the running image, or use docker container ls to obtain the container details. Just add a simple Python file with a handler function for the sake of completion. Extract the lib files from it to your computer.Create a Docker container with Lambda dependencies.So, how can we extract the Lambda compatible libraries? There can be other ways, but I took the Docker image approach, which is explained below. I will explain the first option: packaging with the Lambda itself. It doesn’t matter how you do this, both approaches will work without an issue. We need to upload the correct libraries which are compatible with the Lambda environment along with the Lambda package, or we can create a Lambda layer with those libraries. When you upload these to the Lambda package, it will give all kinds of errors and it took me some time to understand what was going on. But Lambda functions run on a Linux-based environment, and these libraries don’t work there. I have a Mac for my development and it bundles the MacOS specific libraries for the package. And these libraries are taken from your computer and get bundled into the package. So, we must have these C++ library files in the Lambda package we upload, for it to work properly. It uses C++ behind the scene to perform all the hard work. The original PIL was discontinued and now it’s called Pillow. Python Imaging Library (PIL) is a Python library that adds a lot of functionality for image manipulation. This block of code works locally without an issue! But this probably won’t work on a Lambda function. This function will take an image as a binary object and resize it if the width is higher than our predefined max. Resized_image.save(img_byte_arr, format='JPEG') # Write the image into a new bytes object Resized_image = image.resize((BASE_WIDTH, h_size), Image.NEAREST) H_size = int((float(image.height) * float(w_percent))) W_percent = (BASE_WIDTH / float(image.width)) # Resize the image maintaining the aspect ratio # Return the original image if it's already smaller Image = Image.open(BytesIO(binary_image)) import osīASE_WIDTH = int(os.environ.get("MAX_IMAGE_WIDTH", 2000)) Nope! But let’s first see the code change we have to do. We just need to add pillow into the requirements file, do the code change, and boom! Right? We use PIL: the thirdparty library used for image manipulation in Python. Well, the code change is quite straightforward. So, why am I writing an article about it? Because, there is a catch! The code change The next option is to resize the image before serving. So, how can we fix this? We couldn’t directly serve from S3 because we have a custom logic to map the requested URL into the path of the file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |