Cloud Native 9 min read

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.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
Turn Images into Sketches with Alibaba Cloud Serverless Functions

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.

Create Service UI
Create Service UI

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.

Create Function UI
Create Function UI

Configure the trigger: choose OSS, select the input bucket ( sketch-image-input), and filter for .jpg files to avoid unintended triggers.

OSS Trigger Settings
OSS Trigger Settings

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.

Log view in SLS
Log view in SLS

Verify the result by browsing the output bucket; the sketch images appear as expected.

Sketch image in output bucket
Sketch image in output bucket

Batch uploading multiple photos confirms that the function handles concurrent triggers reliably.

Batch processing result
Batch processing result

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ServerlessPythonImage ProcessingOSSOpenCVAlibaba CloudFunction Compute
Alibaba Cloud Native
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.