Development of Image-to-Image generation and styling
Image-to-Image (img2img) transforms the original image based on a text description: stylizing a photo to resemble a painting, changing the background, modifying objects, and redesigning the image. The denoising_strength parameter controls the degree of change (0 = no change, 1 = complete replacement).
Basic 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()
Stylization for artistic styles
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 for style reference
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()
Applications by industry
| Industry | Application | Strength |
|---|---|---|
| E-commerce | Change product background | 0.4–0.6 |
| Media | Photo styling for an article | 0.5–0.7 |
| Gamedev | Concept art from a sketch | 0.6–0.8 |
| Architecture | Visualization from drawing | 0.5–0.7 |
| Fashion | Change of color/texture of clothes | 0.3–0.5 |
Timeframe: img2img API – 1–2 days. A full-fledged styling service with presets and a web interface – 1–2 weeks.







