mirror of
https://github.com/brycedrennan/imaginAIry
synced 2024-10-31 03:20:40 +00:00
38c7f88950
Specify advanced text based masks using boolean logic and strength modifiers. Mask descriptions must be lowercase. Keywords uppercase. Valid symbols: `AND`, `OR`, `NOT`, `()`, and mask strength modifier `{*1.5}` where `+` can be any of `+ - * /`. Single-character boolean operators also work. When writing strength modifies know that pixel values are between 0 and 1. - feature: apply mask edits to original files - feature: auto-rotate images if exif data specifies to do so - fix: accept mask images in command line
60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
from typing import Sequence
|
|
|
|
import numpy as np
|
|
import PIL
|
|
import torch
|
|
from einops import rearrange, repeat
|
|
from PIL import Image
|
|
|
|
from imaginairy.utils import get_device
|
|
|
|
|
|
def pillow_fit_image_within(image: PIL.Image.Image, max_height=512, max_width=512):
|
|
image = image.convert("RGB")
|
|
w, h = image.size
|
|
resize_ratio = min(max_width / w, max_height / h)
|
|
w, h = int(w * resize_ratio), int(h * resize_ratio)
|
|
w, h = map(lambda x: x - x % 64, (w, h)) # resize to integer multiple of 64
|
|
image = image.resize((w, h), resample=Image.Resampling.NEAREST)
|
|
return image, w, h
|
|
|
|
|
|
def pillow_img_to_torch_image(img: PIL.Image.Image):
|
|
img = img.convert("RGB")
|
|
img = np.array(img).astype(np.float32) / 255.0
|
|
img = img[None].transpose(0, 3, 1, 2)
|
|
img = torch.from_numpy(img)
|
|
return 2.0 * img - 1.0
|
|
|
|
|
|
def pillow_img_to_opencv_img(img: PIL.Image.Image):
|
|
open_cv_image = np.array(img)
|
|
# Convert RGB to BGR
|
|
open_cv_image = open_cv_image[:, :, ::-1].copy()
|
|
return open_cv_image
|
|
|
|
|
|
def model_latents_to_pillow_imgs(latents: torch.Tensor) -> Sequence[PIL.Image.Image]:
|
|
from imaginairy.api import load_model # noqa
|
|
|
|
model = load_model()
|
|
latents = model.decode_first_stage(latents)
|
|
latents = torch.clamp((latents + 1.0) / 2.0, min=0.0, max=1.0)
|
|
imgs = []
|
|
for latent in latents:
|
|
latent = 255.0 * rearrange(latent.cpu().numpy(), "c h w -> h w c")
|
|
img = Image.fromarray(latent.astype(np.uint8))
|
|
imgs.append(img)
|
|
return imgs
|
|
|
|
|
|
def pillow_img_to_model_latent(model, img, batch_size=1, half=True):
|
|
# init_image = pil_img_to_torch(img, half=half).to(device)
|
|
init_image = pillow_img_to_torch_image(img).to(get_device())
|
|
init_image = repeat(init_image, "1 ... -> b ...", b=batch_size)
|
|
if half:
|
|
return model.get_first_stage_encoding(
|
|
model.encode_first_stage(init_image.half())
|
|
)
|
|
return model.get_first_stage_encoding(model.encode_first_stage(init_image))
|