All Products
Search
Document Center

Object Storage Service:Indexed slice

Last Updated:Mar 20, 2026

Indexed slice evenly divides a source image into equal-width or equal-height partitions along a specified axis, then returns the partition at a given index. Use this to extract a specific tile from a large image—for example, in image carousels, sprite sheet distribution, or map tile workflows.

Parameters

Action: indexcrop

Syntax: indexcrop,x_{width},i_{index} or indexcrop,y_{height},i_{index}

ParameterDescriptionValid values
xPartition width for horizontal slicing. Cannot be combined with y.1 to image width
yPartition height for vertical slicing. Cannot be combined with x. If both x and y are specified with valid values, y takes effect.1 to image height
iZero-based index of the partition to return. If the index exceeds the number of partitions, OSS returns the source image.0 to (number of partitions − 1). Default: 0

Slice an image

Choose a method based on your image's access control:

  • Public image (public-read or public-read-write ACL): Add the processing parameters to the image URL.

  • Private image: Use an OSS SDK or the REST API. For an overview of available methods, see Image processing methods.

Slice a public image

The following examples use example.jpg stored in the oss-console-img-demo-cn-hangzhou bucket in the China (Hangzhou) region.

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

Source image

Example 1: Horizontal slice — extract the first partition

Parameters: indexcrop,x_100,i_0

  1. Set x_100 to divide the image into 100 px-wide partitions horizontally.

  2. Set i_0 to return the first partition (index 0).

https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/indexcrop,x_100,i_0
Horizontal slice, first partition

Use this pattern to extract individual frames from a sprite sheet or tiles from a horizontal image strip.

Example 2: Vertical slice — index out of range

Parameters: indexcrop,y_100,i_10

  1. Set y_100 to divide the image into 100 px-tall partitions vertically.

  2. Set i_10 to request the 11th partition (index 10).

https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/indexcrop,y_100,i_10

Because the image height does not produce 11 partitions, i=10 exceeds the valid range. OSS returns the source image.

Vertical slice, index out of range — source image returned

Slice a private image

Use an OSS SDK or the REST API to process private images. For more information about how to slice a private image by using other SDKs, see SDK overview.

Use OSS SDKs

Java

Requires OSS SDK for Java 3.17.4 or later.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;

public class Demo {
    public static void main(String[] args) throws Throwable {
        // Specify the endpoint for the China (Hangzhou) region.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the region where the bucket is located.
        String region = "cn-hangzhou";
        // Load credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        String bucketName = "examplebucket";
        String objectName = "example.jpg";
        // Local path to save the processed image.
        String pathName = "D:\\dest.jpg";

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

        try {
            // Slice horizontally into 100 px partitions and return the first one.
            String image = "image/indexcrop,x_100,i_0";
            GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
            request.setProcess(image);
            ossClient.getObject(request, new File(pathName));
        } catch (OSSException oe) {
            System.out.println("Error Message: " + oe.getErrorMessage());
            System.out.println("Error Code: " + oe.getErrorCode());
            System.out.println("Request ID: " + oe.getRequestId());
            System.out.println("Host ID: " + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Error Message: " + ce.getMessage());
        } finally {
            if (ossClient != null) {
                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;

// Load credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
$provider = new EnvironmentVariableCredentialsProvider();
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket = "examplebucket";
$object = "example.jpg";
$download_file = "D:\\dest.jpg";

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

// Slice horizontally into 100 px partitions and return the first one.
$image = "image/indexcrop,x_100,i_0";

$options = array(
    OssClient::OSS_FILE_DOWNLOAD => $download_file,
    OssClient::OSS_PROCESS => $image
);

$ossClient->getObject($bucket, $object, $options);

Python

Requires OSS SDK for Python 2.18.4 or later.

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

# Load credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
region = 'cn-hangzhou'
bucket = oss2.Bucket(auth, endpoint, 'examplebucket', region=region)

key = 'example.jpg'
new_pic = 'D:\\dest.jpg'

# Slice horizontally into 100 px partitions and return the first one.
image = 'image/indexcrop,x_100,i_0'
bucket.get_object_to_file(key, new_pic, process=image)

Go

Requires OSS SDK for Go 3.0.2 or later.

package main

import (
    "fmt"
    "os"

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

func HandleError(err error) {
    fmt.Println("Error:", err)
    os.Exit(-1)
}

func main() {
    // Load credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        HandleError(err)
    }

    client, err := oss.New(
        "https://oss-cn-hangzhou.aliyuncs.com", "", "",
        oss.SetCredentialsProvider(&provider),
        oss.AuthVersion(oss.AuthV4),
        oss.Region("cn-hangzhou"),
    )
    if err != nil {
        HandleError(err)
    }

    bucket, err := client.Bucket("examplebucket")
    if err != nil {
        HandleError(err)
    }

    sourceImageName := "example.jpg"
    targetImageName := "D://dest.jpg"

    // Slice horizontally into 100 px partitions and return the first one.
    image := "image/indexcrop,x_100,i_0"
    err = bucket.GetObjectToFile(sourceImageName, targetImageName, oss.Process(image))
    if err != nil {
        HandleError(err)
    }
}

REST API

Add the indexed slice parameters to the GetObject request. For signature requirements, see (Recommended) Include a V4 signature.

GET /oss.jpg?x-oss-process=image/indexcrop,x_100,i_0 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