Turn Images into Sketches with Alibaba Cloud Serverless Functions
This guide shows how to deploy a Python‑based image‑to‑sketch function on Alibaba Cloud Serverless Function Compute, automatically converting photos uploaded to one OSS bucket into sketch images stored in another bucket, covering prerequisites, environment setup, code implementation, deployment steps, testing, and troubleshooting.
Scenario Introduction
The article demonstrates how to deploy a simple Python function on Alibaba Cloud Serverless Function Compute that converts ordinary photos into sketch‑style images. Uploaded pictures in a designated OSS bucket are processed automatically and the resulting sketches are saved to a separate OSS bucket.
Related Concepts
Serverless (Serverless computing) abstracts away server management, allowing developers to focus on business logic. Function Compute (FC) is Alibaba Cloud’s FaaS offering, often combined with BaaS services such as OSS, SLS, and RDS to build complete applications.
Prerequisites
Alibaba Cloud account with Function Compute, OSS, and SLS services enabled.
Python code that converts a normal image to a sketch (uses OpenCV).
Test images ready for upload.
Two OSS buckets: one for input images (e.g., sketch-image-input) and one for output sketches (e.g., sketch-image-output). The output bucket must be different to avoid infinite trigger loops.
Step 1: Create a Function Compute Service
In the Alibaba Cloud console, select Function Compute and click “Create Service”. Provide a service name and description.
Enter the service, then click “Create Function”. Choose Python 3.6 as the runtime, select “Upload code” and use the sample code template. Set the handler to process event requests.
Configure the trigger: choose OSS, select the input bucket ( sketch-image-input), and filter for .jpg files to avoid unintended triggers.
Step 2: Implement the Sketch Function
Write the Python script that reads an image, applies OpenCV operations to produce a sketch, and saves the result.
# -*- coding: utf-8 -*-
import cv2
import json
import logging
import oss2
def sketch_image(source_image, target_image):
# read image
image = cv2.imread(source_image)
# sketch image
grey_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
invert_image = cv2.bitwise_not(grey_image)
gaussian_blur_image = cv2.GaussianBlur(invert_image, (7, 7), 0)
inverse_image = cv2.bitwise_not(gaussian_blur_image)
sketch_image_result = cv2.divide(grey_image, inverse_image, scale=256.0)
# save image
cv2.imwrite(target_image, sketch_image_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
def handler(event, context):
logger = logging.getLogger()
evt = json.loads(event)
creds = context.credentials
# Required by OSS SDK
auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
evt = evt['events'][0]
bucket_name = evt['oss']['bucket']['name']
endpoint = 'oss-' + evt['region'] + '.aliyuncs.com'
bucket_input = oss2.Bucket(auth, endpoint, bucket_name)
bucket_output = oss2.Bucket(auth, endpoint, 'sketch-image-output')
logger.info('oss endpoint: %s' % endpoint)
objectName = evt['oss']['object']['key']
logger.info('oss objectName: %s' % objectName)
newKey = 'sketch_' + objectName
# Download image
source_image = '/tmp/%s' % objectName
bucket_input.get_object_to_file(objectName, source_image)
logger.info('download image %s success.' % source_image)
# Sketch image
target_image = '/tmp/%s' % newKey
sketch_image(source_image, target_image)
# Upload image
with open(target_image, 'rb') as fileobj:
bucket_output.put_object(newKey, fileobj)
logger.info('upload image %s success.' % newKey)Step 3: Deploy and Test
After uploading the function code, enable SLS logging for easier debugging. Upload a test JPEG to the input bucket; the function will be triggered, process the image, and place the sketch in the output bucket.
Verify the result by browsing the output bucket; the sketch images appear as expected.
Batch uploading multiple photos confirms that the function handles concurrent triggers reliably.
References
• Alibaba Cloud FC OSS trigger SDK: https://help.aliyun.com/document_detail/74765.html
• OSS file download/upload SDK: https://help.aliyun.com/document_detail/88426.html
• FC Python built‑in modules: https://help.aliyun.com/document_detail/158208.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Native
We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
