Segmind Virtual Try-On API
The SegmindVTONAdapter provides an interface to Segmind's Try-On Diffusion API for generating realistic virtual try-on images.
Overview
Segmind's Try-On Diffusion API combines a model image (person) with a cloth image (garment/product) to create realistic virtual try-on results. The adapter handles authentication, image preparation, and response decoding automatically.
API Endpoint: https://api.segmind.com/v1/try-on-diffusion
Reference: Segmind API Documentation
Installation
No additional installation required. The adapter uses the requests library which is included with OpenTryOn.
Authentication
Segmind requires an API key for authentication. You can provide it in two ways:
-
Environment Variable (Recommended):
export SEGMIND_API_KEY="your_api_key" -
Constructor Parameter:
adapter = SegmindVTONAdapter(api_key="your_api_key")
Quick Start
from dotenv import load_dotenv
load_dotenv()
from tryon.api import SegmindVTONAdapter
# Initialize adapter (uses SEGMIND_API_KEY from environment)
adapter = SegmindVTONAdapter()
# Generate virtual try-on images
images = adapter.generate_and_decode(
model_image="data/person.jpg",
cloth_image="data/shirt.jpg",
category="Upper body"
)
# Save results
for idx, image in enumerate(images):
image.save(f"outputs/result_{idx}.png")
API Reference
Class: SegmindVTONAdapter
Adapter class for Segmind Try-On Diffusion API.
Constructor
SegmindVTONAdapter(api_key: Optional[str] = None)
Parameters:
api_key(str, optional): Segmind API key. Defaults toSEGMIND_API_KEYenvironment variable. If not provided, raisesValueError.
Raises:
ValueError: If API key is not provided via parameter or environment variable.
Example:
# Using environment variable
import os
os.environ['SEGMIND_API_KEY'] = 'your_api_key'
adapter = SegmindVTONAdapter()
# Using parameter
adapter = SegmindVTONAdapter(api_key="your_api_key")
Methods
generate(model_image, cloth_image, category="Upper body", num_inference_steps=None, guidance_scale=None, seed=None, base64=True, **kwargs)
Generate virtual try-on image(s) using Segmind API.
Parameters:
model_image(str or io.BytesIO): Model/person image in one of these formats:- File path (str): Path to local image file
- URL (str): HTTP/HTTPS URL to image
- File-like object (io.BytesIO): BytesIO or similar
- Base64 string (str): Base64-encoded image
cloth_image(str or io.BytesIO): Cloth/garment image in same formats asmodel_imagecategory(str): Garment category. Options:"Upper body","Lower body","Dress". Default:"Upper body"num_inference_steps(int, optional): Number of denoising steps. Default:25. Range: 20-100guidance_scale(float, optional): Scale for classifier-free guidance. Default:2. Range: 1-25seed(int, optional): Seed for image generation. Default:-1. Range: -1 to 999999999999999base64(bool): Whether to return base64-encoded image. Default:True**kwargs: Additional parameters for Segmind API
Returns:
Union[str, bytes]: Generated image data:- If
base64=True: Base64-encoded string - If
base64=False: Raw image bytes
- If
Raises:
ValueError: If required parameters are missing, API returns an error, or response format is unexpected
Example:
# Using file paths
image_data = adapter.generate(
model_image="person.jpg",
cloth_image="hoodie.jpg",
category="Upper body"
)
# Using URLs
image_data = adapter.generate(
model_image="https://example.com/person.jpg",
cloth_image="https://example.com/garment.jpg",
category="Lower body"
)
# With custom parameters
image_data = adapter.generate(
model_image="person.jpg",
cloth_image="dress.jpg",
category="Dress",
num_inference_steps=35,
guidance_scale=2.5,
seed=42
)
generate_and_decode(model_image, cloth_image, category="Upper body", num_inference_steps=None, guidance_scale=None, seed=None, **kwargs)
Generate virtual try-on images and decode them to PIL Image objects.
Parameters:
model_image(str or io.BytesIO): Model/person image (same formats asgenerate())cloth_image(str or io.BytesIO): Cloth/garment image (same formats asgenerate())category(str): Garment category. Options:"Upper body","Lower body","Dress". Default:"Upper body"num_inference_steps(int, optional): Number of denoising steps. Default:25guidance_scale(float, optional): Guidance scale. Default:2seed(int, optional): Seed for generation. Default:-1**kwargs: Additional parameters
Returns:
List[PIL.Image.Image]: List of PIL Image objects
Raises:
ValueError: If API returns an error or response format is unexpected
Example:
images = adapter.generate_and_decode(
model_image="data/person.jpg",
cloth_image="data/garment.jpg",
category="Upper body",
num_inference_steps=35,
guidance_scale=2.5,
seed=42
)
# Save all results
for idx, image in enumerate(images):
image.save(f"outputs/vton_result_{idx}.png")
create_virtual_try_on_payload(model_image, cloth_image, category="Upper body", num_inference_steps=None, guidance_scale=None, seed=None, base64=False, **kwargs)
Create the payload for virtual try-on request based on Segmind API format.
Parameters:
model_image(str): Model/person image (URL or base64)cloth_image(str): Cloth/garment image (URL or base64)category(str): Garment category. Default:"Upper body"num_inference_steps(int, optional): Number of denoising stepsguidance_scale(float, optional): Guidance scaleseed(int, optional): Seed for generationbase64(bool): Whether to return base64 output. Default:False**kwargs: Additional parameters
Returns:
dict: API request payload dictionary
Note: This is a low-level method. Use generate() or generate_and_decode() for most use cases.
Garment Categories
Segmind supports three garment categories:
"Upper body": Tops, shirts, jackets, hoodies (default)"Lower body": Pants, skirts, shorts"Dress": Dresses, full-body garments
Inference Parameters
num_inference_steps
Number of denoising steps during generation. More steps generally produce higher quality results but take longer.
- Default:
25 - Range: 20-100
- Recommended: 25-35 for good quality/speed balance
guidance_scale
Scale for classifier-free guidance. Higher values make the model follow the input more closely.
- Default:
2 - Range: 1-25
- Recommended: 2-3 for natural results
seed
Seed for random number generation. Use the same seed to reproduce results.
- Default:
-1(random) - Range: -1 to 999999999999999
- Use case: Set a specific seed for reproducible results
Image Input Formats
The adapter supports multiple input formats:
File Paths
images = adapter.generate_and_decode(
model_image="data/person.jpg",
cloth_image="data/garment.jpg"
)
URLs
images = adapter.generate_and_decode(
model_image="https://example.com/person.jpg",
cloth_image="https://example.com/garment.jpg"
)
File-like Objects
from io import BytesIO
with open("person.jpg", "rb") as f:
person_bytes = BytesIO(f.read())
with open("garment.jpg", "rb") as f:
garment_bytes = BytesIO(f.read())
images = adapter.generate_and_decode(
model_image=person_bytes,
cloth_image=garment_bytes
)
Base64 Strings
import base64
with open("person.jpg", "rb") as f:
person_b64 = base64.b64encode(f.read()).decode()
with open("garment.jpg", "rb") as f:
garment_b64 = base64.b64encode(f.read()).decode()
images = adapter.generate_and_decode(
model_image=person_b64,
cloth_image=garment_b64
)
Complete Example
from dotenv import load_dotenv
load_dotenv()
from tryon.api import SegmindVTONAdapter
from PIL import Image
# Initialize adapter
adapter = SegmindVTONAdapter()
# Generate virtual try-on
images = adapter.generate_and_decode(
model_image="data/person.jpg",
cloth_image="data/shirt.jpg",
category="Upper body",
num_inference_steps=35,
guidance_scale=2.5,
seed=42
)
# Process results
for idx, image in enumerate(images):
# Save image
image.save(f"outputs/vton_result_{idx}.png")
# Display image (if in Jupyter)
# display(image)
# Get image info
print(f"Image {idx}: {image.size} ({image.mode})")
Error Handling
The adapter raises ValueError for common errors:
try:
images = adapter.generate_and_decode(
model_image="person.jpg",
cloth_image="garment.jpg"
)
except ValueError as e:
print(f"Error: {e}")
# Handle error...
Common errors:
- Missing API key
- Invalid image format
- API request failure
- Invalid parameters
Best Practices
Use Environment Variables
Store your API key securely using environment variables:
# .env file
SEGMIND_API_KEY=your_api_key_here
from dotenv import load_dotenv
load_dotenv()
adapter = SegmindVTONAdapter() # Uses environment variable
Image Preprocessing
For best results:
- Use high-resolution images (at least 512x512)
- Ensure person image shows full body or relevant body part
- Use clear, well-lit images
- Remove background if possible
Parameter Tuning
- Start with default parameters (
num_inference_steps=25,guidance_scale=2) - Increase
num_inference_stepsfor higher quality (slower) - Adjust
guidance_scalebased on desired adherence to input - Use
seedfor reproducible results during development