Basic Image Processing

Last updated: 2021-06-09 14:16:35

    Overview

    This document provides an overview of APIs and SDK code samples related to basic image processing.

    Service Feature Description
    Basic image processing service Scaling Proportional scaling, scaling image to target width and height, and more
    Cropping Cut (regular cropping), crop (scaling and cropping), iradius (inscribed circle cropping), and scrop (smart cropping)
    Rotation Adaptive rotation and common rotation
    Format conversion Format conversion, GIF optimization, and progressive display
    Quality conversion Changes the quality of images in JPG and WEBP formats
    Gaussian blur Blurs images
    Sharpening Sharpens images
    Watermarking Image watermarks, text watermarks
    Obtaining image information Basic information, EXIF data, average hue
    Removing metadata Includes EXIF data
    Quick thumbnail template Performs quick format conversion, scaling, and cropping to generate thumbnails

    SDK API Reference

    For the parameters and method description of all the APIs in the SDK, see Api Documentation.

    Processing an Image upon the Upload

    The following example shows how to automatically process an image when you upload it to COS.

    Upon successful upload, COS will save both the original and processed images. You can obtain the processing result using a common download request.

    Sample code

    cs
    PutObjectRequest request = new PutObjectRequest(bucket, key, srcPath);
    JObject o = new JObject();
    // Do not return the original image
    o["is_pic_info"] = 0;
    JArray rules = new JArray();
    JObject rule = new JObject();
    rule["bucket"] = bucket;
    rule["fileid"] = "desample_photo.jpg";
    // Processing parameters. For the rules, please see https://cloud.tencent.com/document/product/436/44879
    rule["rule"] = "imageMogr2/thumbnail/400x400";
    rules.Add(rule);
    o["rules"] = rules;
    string ruleString = o.ToString(Formatting.None);
    request.SetRequestHeader("Pic-Operations", ruleString);
    // Perform the request
    PutObjectResult result = cosXml.PutObject(request);
    
    Note:

    For the complete sample, please go to GitHub.

    Processing an In-Cloud Image

    The following example shows how to process an in-cloud image and store the processing result in COS.

    Sample code

    cs
    JObject o = new JObject();
    // Do not return the original image
    o["is_pic_info"] = 0;
    JArray rules = new JArray();
    JObject rule = new JObject();
    rule["bucket"] = bucket;
    rule["fileid"] = "desample_photo.jpg";
    // Processing parameters. For the rules, please see https://intl.cloud.tencent.com/document/product/436/44879?from_cn_redirect=1
    rule["rule"] = "imageMogr2/thumbnail/400x400";
    rules.Add(rule);
    o["rules"] = rules;
    string ruleString = o.ToString(Formatting.None);
    ImageProcessRequest request = new ImageProcessRequest(bucket, key, ruleString);
    ImageProcessResult result = cosXml.ImageProcess(request);
    
    Note:

    For the complete sample, please go to GitHub.

    Processing an Image upon the Download

    The following sample shows how to process an image stored in COS upon the download:

    Sample code

    cs
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key, localDir, localFileName);
    // Processing parameters. This sample converts the image into TPG format. For the rules, please see https://intl.cloud.tencent.com/document/product/436/44879?from_cn_redirect=1
    getObjectRequest.SetQueryParameter("imageMogr2/format/tpg", null);
    
    Note:

    For the complete sample, please go to GitHub.