Spaces:
Sleeping
Sleeping
File size: 2,278 Bytes
c2b172c 7e5bed5 c2b172c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
from PIL import Image
import numpy as np
import cv2
#first stage sketch preprocess
def conventional_resize(img):
original_width, original_height = img.size
aspect_ratio = original_width / original_height
conventional_sizes = {
1: (1024, 1024),
4/3: (1152, 896),
3/2: (1216, 832),
16/9: (1344, 768),
21/9: (1568, 672),
3/1: (1728, 576),
1/4: (512, 2048),
1/3: (576, 1728),
9/16: (768, 1344),
2/3: (832, 1216),
3/4: (896, 1152)
}
closest_aspect_ratio = min(conventional_sizes.keys(), key=lambda x: abs(x - aspect_ratio)) #1.333333333 #
new_width, new_height = conventional_sizes[closest_aspect_ratio]
resized_img = img.resize((new_width, new_height), Image.LANCZOS)
return resized_img
def get_substrate(img, color=(255, 255, 255, 255)):
size=img.size
substrate = Image.new("RGBA", size, color)
return substrate.convert("RGB")
def sketch_process(img):
substrate=conventional_resize(get_substrate(img))
resized_img = conventional_resize(img)
return substrate, resized_img
#first stage prompt preprocess
def remove_duplicates(base_prompt):
prompt_list = base_prompt.split(", ")
seen = set()
unique_tags = []
for tag in prompt_list :
tag_clean = tag.lower().strip()
if tag_clean not in seen and tag_clean != "":
unique_tags.append(tag)
seen.add(tag_clean)
return ", ".join(unique_tags)
def remove_color(base_prompt):
prompt_list = base_prompt.split(", ")
color_list = ["pink", "red", "orange", "brown", "yellow", "green", "blue", "purple", "blonde", "colored skin", "white hair"]
cleaned_tags = [tag for tag in prompt_list if all(color.lower() not in tag.lower() for color in color_list)]
return ", ".join(cleaned_tags)
def execute_prompt(base_prompt):
prompt_list = base_prompt.split(", ")
execute_tags = ["sketch", "transparent background"]
filtered_tags = [tag for tag in prompt_list if tag not in execute_tags]
return ", ".join(filtered_tags)
def prompt_preprocess(prompt):
result=execute_prompt(prompt)
result=remove_duplicates(result)
result=remove_color(result)
return result
|