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 дні.







