AI-раскрашивание чёрно-белых изображений
AI-раскрашивание восстанавливает цвет в чёрно-белых фотографиях и видео. Нейросетевые модели понимают контекст: небо синее, трава зелёная, кожа телесная — без ручной разметки.
DeOldify — классика раскрашивания
from deoldify import device
from deoldify.device_id import DeviceId
from deoldify.visualize import get_image_colorizer
import PIL.Image as Image
import io
device.set(device=DeviceId.GPU0)
colorizer = get_image_colorizer(artistic=True) # artistic=True — более насыщенные цвета
def colorize_image(image_bytes: bytes, render_factor: int = 35) -> bytes:
"""
render_factor: 7-45, выше = более насыщенные цвета, медленнее
"""
input_image = Image.open(io.BytesIO(image_bytes)).convert("L").convert("RGB")
# Сохраняем временно
import tempfile, os
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
input_image.save(f.name)
temp_path = f.name
result = colorizer.get_transformed_image(temp_path, render_factor=render_factor)
os.unlink(temp_path)
buf = io.BytesIO()
result.save(buf, format="JPEG", quality=95)
return buf.getvalue()
Stable Diffusion img2img подход
from diffusers import StableDiffusionImg2ImgPipeline
import torch
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16
).to("cuda")
def colorize_with_sd(bw_image: bytes, prompt_hint: str = "") -> bytes:
init_image = Image.open(io.BytesIO(bw_image)).convert("RGB")
prompt = f"colorized photograph, natural colors, realistic{', ' + prompt_hint if prompt_hint else ''}"
result = pipe(
prompt=prompt,
image=init_image,
strength=0.5, # Низкий strength сохраняет структуру
guidance_scale=8.0,
num_inference_steps=30
).images[0]
buf = io.BytesIO()
result.save(buf, format="JPEG", quality=95)
return buf.getvalue()
DeOldify лучше для исторических фотографий (обучен на реальных ЧБ снимках). SD img2img даёт больше контроля через промпт (можно задать эпоху, регион). Для видео: DeOldify поддерживает пофреймовое раскрашивание с temporal consistency. Сроки интеграции — 1–2 дня.







