byted-tos-image-process
Bytedance TOS Image Process Skill
This skill provides essential image processing functions for files stored in Bytedance's TOS (TeraObjectStore). It allows you to retrieve image metadata, convert formats, resize, and apply watermarks directly using the Volcengine TOS SDK.
Quick Start
1. Client Initialization
The following Python snippet demonstrates how to initialize the TosClientV2 from environment variables.
import os
import tos
from tos.exceptions import TosClientError, TosServerError
def create_client() -> tos.TosClientV2:
"""Initializes a TosClientV2 using AK/SK (and optional STS token) from environment variables."""
try:
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = os.getenv('TOS_ENDPOINT')
region = os.getenv('TOS_REGION')
security_token = os.getenv('TOS_SECURITY_TOKEN') # Optional, for STS
if not all([ak, sk, endpoint, region]):
raise ValueError("Required environment variables are missing (AK, SK, Endpoint, Region).")
return tos.TosClientV2(
ak=ak,
sk=sk,
endpoint=endpoint,
region=region,
security_token=security_token,
)
except (ValueError, ImportError) as e:
print(f"Error initializing client: {e}")
return None
# Create the client
client = create_client()
2. Basic Workflow
# (Assumes 'client' is initialized and 'bucket_name', 'object_key' are set)
# 1. Get Image Info
try:
response = client.get_object(bucket_name, object_key, process="image/info")
info_data = response.read()
print("Image Info:", info_data.decode('utf-8'))
except TosServerError as e:
print(f"Error getting image info: {e}")
# 2. Resize an Image and save locally
try:
client.get_object_to_file(
bucket_name,
object_key,
"resized_image.jpg",
process="image/resize,w_500,m_lfit" # Resize to 500px width, lfit mode
)
print("Resized image saved to resized_image.jpg")
except TosServerError as e:
print(f"Error resizing image: {e}")
# 3. Convert Image to WebP and save back to TOS
try:
response = client.get_object(
bucket_name,
object_key,
process="image/format,f_webp,q_80", # Convert to WebP, quality 80
save_bucket="my-output-bucket",
save_object="processed/image.webp"
)
save_result = response.read()
print("Converted image saved to TOS:", save_result.decode('utf-8'))
except TosServerError as e:
print(f"Error saving converted image to TOS: {e}")
Core Operations
All image processing is achieved by passing a process string to the get_object or get_object_to_file SDK methods.
1. Get Image Info (ImageInfo)
Retrieves metadata of an image file, such as format, dimensions, and EXIF data.
SDK Method: client.get_object(..., process="image/info")
response = client.get_object(bucket_name, object_key, process="image/info")
image_metadata = response.read().decode('utf-8')
print(image_metadata)
2. Convert Image Format (ImageFormat)
Converts an image to a different format (e.g., JPEG, PNG, WebP) and adjusts quality.
SDK Method: client.get_object_to_file(..., process="image/format,f_webp,q_80")
# Convert to PNG format
client.get_object_to_file(
bucket_name,
object_key,
"output.png",
process="image/format,f_png"
)
3. Resize Image (ImageResize)
Resizes an image based on specified width, height, and resizing mode.
SDK Method: client.get_object_to_file(..., process="image/resize,w_800,h_600,m_fill")
# Resize to a maximum width of 1024px, maintaining aspect ratio
client.get_object_to_file(
bucket_name,
object_key,
"resized_1024.jpg",
process="image/resize,w_1024"
)
4. Apply Watermark (ImageWatermark & ImageBlindWatermark)
Adds a visible or blind watermark to an image. Parameters are complex and should be constructed according to the official TOS documentation.
SDK Method: client.get_object_to_file(..., process="image/watermark,...")
# Example for a text watermark (parameters must be Base64-encoded)
# This is a conceptual example. Refer to official docs for exact keys.
import base64
text_b64 = base64.b64encode("My Watermark".encode()).decode()
process_rule = f"image/watermark,type_1,text_{text_b64},size_40,p_9"
client.get_object_to_file(
bucket_name,
object_key,
"watermarked.jpg",
process=process_rule
)
5. Generic Image Processing (ImageProcess)
A flexible entry point that accepts any valid image processing string.
SDK Method: client.get_object(..., process="<full-process-string>")
# Example: Apply a Gaussian blur (hypothetical parameters)
client.get_object_to_file(
bucket_name,
object_key,
"blurred.jpg",
process="image/blur,r_5,s_2"
)
Authorization
Authentication is handled by tos.TosClientV2. Provide credentials via environment variables.
Required Environment Variables
TOS_ACCESS_KEYTOS_SECRET_KEYTOS_ENDPOINTTOS_REGION
Optional for STS
TOS_SECURITY_TOKEN
Best Practices
- Error Handling: Wrap SDK calls in
try...exceptblocks to handleTosClientErrorandTosServerError. - Parameter Construction: For complex operations like watermarking, carefully construct the
processstring according to the official TOS documentation. Base64-encode parameter values where required. - Client Reuse: Initialize the
TosClientV2once and reuse it for multiple operations.
Additional Resources
- For detailed parameters of each operation, see REFERENCE.md.
- For common end-to-end examples, see WORKFLOWS.md.
- For executable Python examples, see the
scripts/directory. - For the definitive list of all processing parameters, always consult the official Volcengine TOS Image Processing documentation.
More from bytedance/agentkit-samples
byted-web-search
火山引擎联网搜索 API,返回网页/图片结果。联网搜索场景优先使用本 skill。触发词包括:查/搜/找、真的吗/靠谱吗/确认/核实、最近/今天/最新/近期、出处/来源/链接、有什么/有哪些/推荐、价格/政策/汇率/行情、对比/区别/哪个好、听说/据说/不太确定、热搜/热门/火、帮我看/了解一下、求证/辟谣、值不值得/该不该。任务依赖在线事实或时效性时优先使用。若回答可能依赖外部事实,优先调用本 skill 再作答。支持 API Key / AK/SK。
369byted-seedream-image-generate
Generate high-quality images from text prompts using Volcano Engine Seedream models. Supports multiple artistic styles and aspect ratios. Use this skill when users want to create images from text descriptions, generate artwork in various styles, create visual content for creative projects, or need AI-powered image generation capabilities.
183byted-las-video-edit
Extracts and clips video segments from long videos using natural language descriptions. AI-powered smart video editing, video trimming, and video cutting powered by Volcengine LAS. Describe what you want — scenes, people, objects, actions, events — and get trimmed clips automatically. Video search and video content retrieval: find and locate specific people, objects, or scenes in footage. Supports reference images for person matching and object matching (search video by image). Two modes: simple (fast) and detail (thorough, optional ASR). Use this skill when the user wants to edit/clip/cut videos using natural language descriptions, extract highlights or key moments from videos, find specific people/objects/scenes in video footage (by text or reference image), compile highlight reels from long videos, trim video segments, or do AI-powered smart video editing.
163byted-las-pdf-parse-doubao
Parses and reads PDF documents into structured Markdown text using Volcengine LAS Doubao AI models. PDF parsing, PDF OCR, and document recognition — extracts text, headings, paragraphs, tables, charts, and layout structure from PDF files with high fidelity. Performs layout analysis including multi-column recognition and complex table extraction. Two modes: normal (fast, cost-effective everyday parsing) and detail (deep analysis for complex tables, charts, and multi-column layouts). Converts PDF to Markdown, PDF to text, and structured data. Digitizes scanned PDF documents and scanned images via OCR. Supports TOS paths, HTTP URLs, and local file upload. Async submit-poll workflow with batch processing support. Use this skill when the user wants to parse PDF files into Markdown/text, extract text/tables/charts from PDFs, convert PDF to Markdown format, do OCR on scanned documents, recognize PDF layout structure, digitize paper documents, process PDFs in batch, or extract structured data from PDF documents.
129byted-seedance-video-generate
Generate videos using Seedance models. Invoke when user wants to create videos from text prompts, images, or reference materials.
109byted-data-search
|
106