#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Seedream API Image Editor - Python Version
Generate new images based on source image, just modify API_KEY to use
"""
# =================== Configuration Section ===================
API_KEY = "sk-" # Replace with your API key
API_URL = "https://api.yelinai.com/v1/images/generations" # Image editing API endpoint
PROMPT = "Generate a close-up image of a dog lying on lush grass." # Editing prompt
IMAGE_URL = "https://ark-doc.tos-ap-southeast-1.bytepluses.com/doc_image/seedream4_imageToimage.png" # Source image URL
MODEL = "seedream-4-0-250828" # Model to use
OUTPUT_DIR = "." # Output directory
# ==============================================
import requests
import os
import datetime
def edit_image(prompt, image_url, output_dir="."):
"""Generate edited image based on source image"""
print(f"🎨 Seedream Image Editor")
print(f"📝 Edit Prompt: {prompt}")
print(f"🖼️ Source URL: {image_url[:50]}...")
print("=" * 50)
# API request parameters
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
payload = {
"model": MODEL,
"prompt": prompt,
"image": image_url,
"sequential_image_generation": "disabled",
"response_format": "url",
"size": "2K",
"stream": False,
"watermark": False
}
try:
print("⏳ Editing image...")
# Call API
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
if response.status_code != 200:
return False, f"API Error ({response.status_code}): {response.text}"
# Parse response
result = response.json()
if "data" not in result or len(result["data"]) == 0:
return False, "API returned no image data"
# Get image URL
edited_image_url = result["data"][0]["url"]
print(f"🌐 Got edited image URL successfully")
# Download image
print("⬇️ Downloading edited image...")
img_response = requests.get(edited_image_url, timeout=30)
img_response.raise_for_status()
# Generate filename with timestamp
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(output_dir, f"edited_{timestamp}.jpg")
# Save image
os.makedirs(output_dir, exist_ok=True)
with open(filename, 'wb') as f:
f.write(img_response.content)
file_size = len(img_response.content)
return True, f"✅ Edit successful: {filename} ({file_size // 1024}KB)"
except Exception as e:
return False, f"Edit failed: {str(e)}"
def main():
"""Main function"""
print("🚀 Seedream API Image Editor - Python Version")
print("=" * 50)
# Check API key
if API_KEY == "sk-" or not API_KEY:
print("⚠️ Please modify API_KEY at the top of the code")
print(" Replace 'sk-' with your actual API key")
print()
print("📋 Instructions:")
print("1. Modify API_KEY to your actual key")
print("2. Modify IMAGE_URL to the image you want to edit")
print("3. Modify PROMPT to describe the desired editing effect")
print("4. Run: python3 seedream-image-edit.py")
return
# Execute image editing
success, message = edit_image(PROMPT, IMAGE_URL, OUTPUT_DIR)
print(message)
if success:
print()
print("🎉 Edit completed!")
print("💡 Tip: Modify PROMPT and IMAGE_URL to edit different images")
if __name__ == "__main__":
main()