Разработка Image-to-Image генерации и стилизации
Image-to-Image (img2img) трансформирует исходное изображение по текстовому описанию: стилизация фото под живопись, изменение фона, модификация объектов, переработка дизайна. Параметр denoising_strength контролирует степень изменений (0=нет изменений, 1=полная замена).
Базовый img2img (diffusers)
from diffusers import StableDiffusionXLImg2ImgPipeline
from PIL import Image
import torch
import io
class Img2ImgService:
def __init__(self):
self.pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0",
torch_dtype=torch.float16,
use_safetensors=True,
variant="fp16"
).to("cuda")
def stylize(
self,
input_image: bytes,
prompt: str,
negative_prompt: str = "low quality, blurry",
strength: float = 0.7, # 0.3–0.5 = лёгкая стилизация, 0.7–0.9 = сильное изменение
steps: int = 30,
guidance_scale: float = 7.5
) -> bytes:
init_image = Image.open(io.BytesIO(input_image)).convert("RGB")
# Размер должен быть кратен 64
w, h = init_image.size
w, h = (w // 64) * 64, (h // 64) * 64
init_image = init_image.resize((w, h))
result = self.pipe(
prompt=prompt,
negative_prompt=negative_prompt,
image=init_image,
strength=strength,
num_inference_steps=steps,
guidance_scale=guidance_scale
).images[0]
buf = io.BytesIO()
result.save(buf, format="PNG")
return buf.getvalue()
Стилизация под художественные стили
STYLE_PROMPTS = {
"oil_painting": "oil painting, thick brushstrokes, impasto technique, museum quality artwork",
"watercolor": "watercolor painting, soft edges, transparent washes, paper texture",
"anime": "anime style, cel shading, vibrant colors, Studio Ghibli aesthetic",
"pencil_sketch": "pencil sketch, graphite drawing, hatching, monochrome",
"pixel_art": "pixel art, 16-bit style, low resolution, retro game aesthetic",
"3d_render": "3D render, octane render, photorealistic, studio lighting, 8k",
"comic_book": "comic book style, bold outlines, halftone pattern, Ben-Day dots",
}
async def apply_artistic_style(
image_bytes: bytes,
style: str,
strength: float = 0.65
) -> bytes:
style_prompt = STYLE_PROMPTS.get(style, style)
return stylize_service.stylize(image_bytes, style_prompt, strength=strength)
IP-Adapter для стиль-референса
from diffusers import StableDiffusionXLPipeline
from diffusers.utils import load_image
import torch
# IP-Adapter переносит стиль/содержимое reference изображения
pipe = StableDiffusionXLPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
torch_dtype=torch.float16
).to("cuda")
pipe.load_ip_adapter(
"h94/IP-Adapter",
subfolder="sdxl_models",
weight_name="ip-adapter_sdxl.bin"
)
pipe.set_ip_adapter_scale(0.6)
def stylize_with_reference(
content_image: bytes,
style_reference: bytes,
prompt: str
) -> bytes:
content = Image.open(io.BytesIO(content_image))
style = Image.open(io.BytesIO(style_reference))
result = pipe(
prompt=prompt,
ip_adapter_image=style, # стиль берём из reference
image=content,
num_inference_steps=30
).images[0]
buf = io.BytesIO()
result.save(buf, format="PNG")
return buf.getvalue()
Применения по отраслям
| Отрасль | Применение | Strength |
|---|---|---|
| E-commerce | Смена фона товара | 0.4–0.6 |
| Медиа | Стилизация фото для статьи | 0.5–0.7 |
| Геймдев | Концепт арт из скетча | 0.6–0.8 |
| Архитектура | Визуализация из чертежа | 0.5–0.7 |
| Мода | Смена цвета/фактуры одежды | 0.3–0.5 |
Сроки: img2img API — 1–2 дня. Полноценный сервис стилизации с пресетами и веб-интерфейсом — 1–2 недели.







