All Products
Search
Document Center

Object Storage Service:Average tone

Last Updated:Mar 19, 2026

The average-hue operation calculates the average color of an image and returns a single hexadecimal RGB value. Use this value for UI theming, content tagging, or image search workflows where a dominant color signal is sufficient.

Parameters

ParameterValueDescription
Actionaverage-hueCalculates the average color of the image
Processing instructionimage/average-huePass as the x-oss-process query parameter or SDK process parameter
Return format0xRRGGBBPlain-text string; RR, GG, and BB are two-digit hexadecimal values for the red, green, and blue channels

Example response:

0xbbcd7f

This equals RGB (187, 205, 127).

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket containing the image you want to analyze

  • For private images: access credentials with the oss:GetObject permission configured as environment variables (OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET)

Query the average hue of a public-read image

For public-read or public-read-write images, append ?x-oss-process=image/average-hue to the image URL.

Example:

Original image URL:

https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg
Original image

Query URL:

https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/average-hue

The browser returns 0xbbcd7f, which corresponds to RGB (187, 205, 127).

Average hue result

Query the average hue of a private image

For private images, use the OSS SDKs or the REST API. All examples pass image/average-hue as the process parameter to the GetObject operation. The response is a plain-text string in 0xRRGGBB format.

Java

Requires OSS SDK for Java 3.17.4 or later.

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyuncs.exceptions.ClientException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Demo {
    public static void main(String[] args) throws ClientException {
        // Specify the endpoint of the region where the bucket is located.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region of the bucket. Example: cn-hangzhou.
        String region = "cn-hangzhou";
        // Load access credentials from environment variables.
        // Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
        EnvironmentVariableCredentialsProvider credentialsProvider =
            CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the bucket name.
        String bucketName = "examplebucket";
        // Specify the object key. For objects not in the root directory, include the full path.
        // Example: exampledir/example.jpg
        String key = "example.jpg";

        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Pass the average-hue processing instruction to GetObject.
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
            getObjectRequest.setProcess("image/average-hue");

            OSSObject ossObject = ossClient.getObject(getObjectRequest);

            // Read and print the result.
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = ossObject.getObjectContent().read(buffer)) != -1) {
                baos.write(buffer, 0, bytesRead);
            }
            System.out.println(baos.toString("UTF-8"));
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            ossClient.shutdown();
        }
    }
}

PHP

Requires OSS SDK for PHP 2.7.0 or later.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;

try {
    // Load access credentials from environment variables.
    // Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
    $provider = new EnvironmentVariableCredentialsProvider();
    // Specify the endpoint of the region where the bucket is located.
    $endpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
    // Specify the bucket name.
    $bucket = 'examplebucket';
    // Specify the object key. For objects not in the root directory, include the full path.
    // Example: exampledir/example.jpg
    $key = 'example.jpg';

    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region" => "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

    // Pass the average-hue processing instruction to getObject.
    $options[$ossClient::OSS_PROCESS] = "image/average-hue";
    $result = $ossClient->getObject($bucket, $key, $options);
    var_dump($result);
} catch (OssException $e) {
    printf($e->getMessage() . "\n");
}

Python

Requires OSS SDK for Python 2.18.4 or later.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Load access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located.
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

# Specify the object key. For objects not in the root directory, include the full path.
# Example: exampledir/example.jpg
key = 'example.jpg'

try:
    # Pass the average-hue processing instruction to get_object.
    result = bucket.get_object(key, process='image/average-hue')
    print(result.read().decode('utf-8'))
except oss2.exceptions.OssError as e:
    print("Error:", e)

Go

Requires OSS SDK for Go 3.0.2 or later.

package main

import (
    "fmt"
    "io"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Load access credentials from environment variables.
    // Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the endpoint of the region where the bucket is located.
    client, err := oss.New(
        "https://oss-cn-hangzhou.aliyuncs.com", "", "",
        oss.SetCredentialsProvider(&provider),
        oss.AuthVersion(oss.AuthV4),
        oss.Region("cn-hangzhou"),
    )
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Pass the average-hue processing instruction to GetObject.
    body, err := bucket.GetObject("example.jpg", oss.Process("image/average-hue"))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    defer body.Close()

    data, err := io.ReadAll(body)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println(string(data))
}

REST API

To call the API directly, include V4 signature calculation in your code. For details, see (Recommended) Include a V4 signature.

Pass the processing instruction as a query parameter in the GetObject request:

GET /oss.jpg?x-oss-process=image/average-hue HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e

What's next

  • To apply additional image processing operations alongside average-hue, see Overview for the full list of supported operations and how to chain them.