banner
月落星河Tsukistar

月落星河Tsukistar

浩瀚中的伟大,孤独间的渺小
github
twitter
youtube
bilibili
email

Design and implementation of a system for dynamically adjusting image size in a serverless environment.

Recently, I chose a topic for my graduation project based on my previous projects and image processing. Serverless is also a popular service that has emerged in recent years, so I chose this topic to research serverless from scratch. (Postscript: The teacher who assigned the topic later realized that the workload for my topic was too small, so they expanded the topic to include document recognition and processing, etc. QwQ)

Reference article: 【AWS Essay】Dynamic Image Resizing Using AWS Serverless Architecture

Development Environment:
Python 3.6 (Tencent Cloud currently only supports this version for Python 3), Tencent Cloud SCF

Steps:

  1. Configure the environment:

    • Create a new Flask framework in Tencent Cloud's "Serverless Framework", fill in the name and region as desired.
    • After deployment, click on "Update Code" in "Development & Deployment".
    • Then choose to download the project locally.
  2. Function development:

    • Currently, the function can process and return images stored in my cloud storage, mainly for ease of calling and parameter reservation.
    • Using the inherent format of the Flask app, two parameters, screen_width (screen width) and pic_url (image path), are reserved.
    • When calling the function, if the screen width is specified, it will return an image with the corresponding width and proportional scaling.
    • If not specified, it will return the original image.
    • The Flask code for this part is provided.
  3. Image processing:

    • The image_input function uses requests to get the image from the URL, wraps it in a response object, and then opens it with the Image library from PIL. The response object needs to be processed with BytesIO.
    • The image_output function converts the image to bytes using BytesIO, saves it, and returns it.
    • The image_resize function uses the resize method from the Image library to scale the image proportionally. Image.ANTIALIAS is used for high-definition parameters.

Encountered problems and solutions:

  1. npm installation encountered rollbackFailedOptional: verb npm-session problem: Used the command npm install -g cnpm --registry=https://registry.npm.taobao.org to replace the official source with the Taobao source, and then used cnpm for installation.

  2. Error occurred when installing the corresponding third-party library: Used the Aliyun mirror: https://mirrors.aliyun.com/pypi/simple/ and changed the related pip code in the serverless.yml file to hook: pip install -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt -t ./

  3. Import error for Pillow: The reason is that the local environment is different from the cloud environment. Even if both are Python 3.6.0, there may be slight differences in third-party libraries between Windows and Linux. The solution is to upload the code and execute pip install -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt -t ./ in the cloud environment to download the dependencies.

Summary:

After going through the entire process, I have a general understanding of the steps to deploy a serverless service. I also realized that since the dependencies of serverless are included with the code, if development and deployment are not done on the same operating system, it is recommended to deploy in the cloud environment after uploading the code to avoid runtime errors.

Changing the size of images is just one small application. In theory, many applications can run and produce results in a serverless environment, such as short links, image recognition, text recognition, etc. The application of serverless will become more and more extensive in the future.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.