2023-05-20 18:33:43 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from PIL import Image, ImageEnhance, ImageStat
|
2023-02-23 09:28:40 +00:00
|
|
|
|
2023-12-10 00:33:39 +00:00
|
|
|
from imaginairy.api import imagine
|
2023-02-23 09:28:40 +00:00
|
|
|
from imaginairy.enhancers.describe_image_blip import generate_caption
|
2023-12-10 00:33:39 +00:00
|
|
|
from imaginairy.schema import ControlInput, ImaginePrompt
|
2023-02-23 09:28:40 +00:00
|
|
|
|
2023-05-20 18:33:43 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2023-02-23 09:28:40 +00:00
|
|
|
|
|
|
|
|
2023-05-20 18:33:43 +00:00
|
|
|
def colorize_img(img, max_width=1024, max_height=1024, caption=None):
|
|
|
|
if not caption:
|
|
|
|
caption = generate_caption(img, min_length=10)
|
|
|
|
caption = caption.replace("black and white", "color")
|
|
|
|
caption = caption.replace("old picture", "professional color photo")
|
|
|
|
caption = caption.replace("vintage photograph", "professional color photo")
|
|
|
|
caption = caption.replace("old photo", "professional color photo")
|
|
|
|
caption = caption.replace("vintage photo", "professional color photo")
|
|
|
|
caption = caption.replace("old color", "color")
|
|
|
|
caption = caption.replace(" old fashioned ", " ")
|
|
|
|
caption = caption.replace(" old time ", " ")
|
|
|
|
caption = caption.replace(" old ", " ")
|
|
|
|
logger.info(caption)
|
|
|
|
control_inputs = [
|
2023-12-08 04:57:55 +00:00
|
|
|
ControlInput(mode="colorize", image=img, strength=2),
|
2023-05-20 18:33:43 +00:00
|
|
|
]
|
|
|
|
prompt_add = ". color photo, sharp-focus, highly detailed, intricate, Canon 5D"
|
2023-02-23 09:28:40 +00:00
|
|
|
prompt = ImaginePrompt(
|
2023-05-20 18:33:43 +00:00
|
|
|
prompt=f"{caption}{prompt_add}",
|
|
|
|
init_image=img,
|
|
|
|
init_image_strength=0.0,
|
|
|
|
control_inputs=control_inputs,
|
2023-12-10 00:33:39 +00:00
|
|
|
size=(min(img.width, max_width), min(img.height, max_height)),
|
2023-02-25 08:14:12 +00:00
|
|
|
steps=30,
|
2023-05-20 18:33:43 +00:00
|
|
|
prompt_strength=12,
|
2023-02-23 09:28:40 +00:00
|
|
|
)
|
2023-09-29 08:13:50 +00:00
|
|
|
result = next(iter(imagine(prompt)))
|
2023-02-23 09:28:40 +00:00
|
|
|
colorized_img = replace_color(img, result.images["generated"])
|
2023-05-20 18:33:43 +00:00
|
|
|
|
|
|
|
# allows the algorithm some leeway for the overall brightness of the image
|
|
|
|
# results look better with this
|
|
|
|
colorized_img = match_brightness(colorized_img, result.images["generated"])
|
|
|
|
|
2023-02-23 09:28:40 +00:00
|
|
|
return colorized_img
|
|
|
|
|
|
|
|
|
|
|
|
def replace_color(target_img, color_src_img):
|
|
|
|
color_src_img = color_src_img.resize(target_img.size)
|
|
|
|
|
|
|
|
_, _, value = target_img.convert("HSV").split()
|
|
|
|
hue, saturation, _ = color_src_img.convert("HSV").split()
|
|
|
|
|
|
|
|
return Image.merge("HSV", (hue, saturation, value)).convert("RGB")
|
2023-05-20 18:33:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def calculate_brightness(image):
|
|
|
|
greyscale_image = image.convert("L")
|
|
|
|
stat = ImageStat.Stat(greyscale_image)
|
|
|
|
return stat.mean[0]
|
|
|
|
|
|
|
|
|
|
|
|
def match_brightness(target_img, source_img):
|
|
|
|
target_brightness = calculate_brightness(target_img)
|
|
|
|
source_brightness = calculate_brightness(source_img)
|
|
|
|
|
|
|
|
brightness_factor = source_brightness / target_brightness
|
|
|
|
|
|
|
|
enhancer = ImageEnhance.Brightness(target_img)
|
|
|
|
return enhancer.enhance(brightness_factor)
|