Split Images Effortlessly with vetkastar/image-slicing Cognitive Actions

21 Apr 2025
Split Images Effortlessly with vetkastar/image-slicing Cognitive Actions

In the world of image processing, developers often require efficient ways to manipulate images for various applications, from web development to data analysis. The vetkastar/image-slicing Cognitive Actions provide powerful capabilities to split images into multiple parts seamlessly. This blog post will guide you through one of the primary actions offered by this spec, detailing its purpose and how to implement it in your applications.

Prerequisites

Before diving into the specifics of the actions, ensure you have the following:

  • An API key for the Cognitive Actions platform to authenticate your requests.
  • A basic understanding of how to make HTTP requests and handle JSON data.
  • Familiarity with Python programming to utilize the provided code examples effectively.

Authentication typically involves passing your API key in the headers of your requests, ensuring secure access to the Cognitive Actions.

Cognitive Actions Overview

Split Images into Four Parts

Description:
This operation splits one or multiple images into four equal parts. It can process individual images or archives containing multiple images. Users have the option to select the output image format and receive the results in a zip archive.

Category: Image Processing

Input

The input for this action requires the following fields:

  • image (string, required): A URI pointing to an input image or a zip archive containing multiple images.
  • returnAsZip (boolean, optional): Determines whether to return the processing result as a zip archive. Defaults to false.
  • outputImageFormat (string, optional): Specifies the format of the output image. Valid options are: png, jpeg, or webp. Defaults to png.

Example Input:

{
  "image": "https://replicate.delivery/pbxt/Lm1OaJ4R1SgY91F5hYMuziHGsPGadPbMH4Q2i2omGQvAmNb6/000a2d38183e668676f04f0cefe3606b.png",
  "returnAsZip": false,
  "outputImageFormat": "png"
}

Output

The action typically returns an array of URIs pointing to the four split images. If requested as a zip, it could return a single URI pointing to the zip file containing the images.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/6260ba14-60b1-4b22-b50f-d8c2746e04ee/586100e6-c40a-45d1-9dd8-18ee57ef44d4.png",
  "https://assets.cognitiveactions.com/invocations/6260ba14-60b1-4b22-b50f-d8c2746e04ee/9ca63792-3d5d-4072-890d-96cb2f25ccb2.png",
  "https://assets.cognitiveactions.com/invocations/6260ba14-60b1-4b22-b50f-d8c2746e04ee/365ec75b-52ca-4815-b0b1-ca3a0e6b193c.png",
  "https://assets.cognitiveactions.com/invocations/6260ba14-60b1-4b22-b50f-d8c2746e04ee/b8903f94-27a1-478b-bdbd-c6a685ba39df.png"
]

Conceptual Usage Example (Python)

Here’s a conceptual Python code snippet to demonstrate how to invoke this action:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint

action_id = "31bfaa7c-a21d-488d-8124-a30c7c4434aa" # Action ID for Split Images into Four Parts

# Construct the input payload based on the action's requirements
payload = {
    "image": "https://replicate.delivery/pbxt/Lm1OaJ4R1SgY91F5hYMuziHGsPGadPbMH4Q2i2omGQvAmNb6/000a2d38183e668676f04f0cefe3606b.png",
    "returnAsZip": False,
    "outputImageFormat": "png"
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload} # Hypothetical structure
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this snippet, the action_id is set to the ID associated with the "Split Images into Four Parts" action. The payload is constructed using the example_input. The requests.post method sends the request to the hypothetical endpoint.

Conclusion

The vetkastar/image-slicing Cognitive Actions provide a streamlined way to split images into four parts, enhancing your application's image processing capabilities. By utilizing the provided action, you can effectively manage and manipulate images with minimal effort. Explore further use cases like batch image processing or integrating with your existing workflows to maximize the utility of these actions!