objektify2 / generate.py
tripleS-Dev
update V1.3.8
5ad9ad5
raw
history blame
19.6 kB
import os
import time
import uuid
import numpy as np
import gradio as gr
import fn
import edimg
import edgif
from PIL import Image, ImageDraw, ImageFont, ImageColor, ImageFilter, ImageSequence, PngImagePlugin
from copy import deepcopy
import maker
import pre
img_blank = Image.open('resource/blank.png')
side = Image.open("resources/side.png")
side_special = Image.open("resources/side_special.png")
side_oma1 = Image.open("resources/side_oma1.png")
side_oma2 = Image.open("resources/side_oma2.png")
def sorting(img_type, img_static, img_dynamic_file, obj_color, obj_color_picker, ai_num, txt_color, txt_color_picker, member_name_dropdown, group_name_radio, group_name_txt, obj_num, obj_alphabet, obj_alphabet_txt, obj_serial, horizontal):
match img_type:
case "Image":
if img_static == None:
gr.Warning("Upload the image first")
return "resource/upload_first.png", "resource/upload_first.png", None, None
img_static = Image.open(img_static)
if not img_static.size == (1164, 1800):
img_static = edimg.resize_img(img_static, 1164, 1800)
print('resized!')
img, img_copy = front_static(img_static, obj_color, obj_color_picker, ai_num, txt_color, txt_color_picker, member_name_dropdown, group_name_radio, group_name_txt, obj_num, obj_alphabet, obj_alphabet_txt, obj_serial)
if horizontal:
img = Image.open(img)
img_rotate = img.rotate(90, expand=True)
return img_rotate, img, None, img_copy
return img, img, None, img_copy
case "Gif":
if img_dynamic_file == None:
gr.Warning("Upload the gif first")
return "resource/upload_first.png", "resource/upload_first.png", None
overlay = front_static(img_blank, obj_color, obj_color_picker, ai_num, txt_color, txt_color_picker, member_name_dropdown, group_name_radio, group_name_txt, obj_num, obj_alphabet, obj_alphabet_txt, obj_serial)
gif = Image.open(img_dynamic_file)
edgif.attach_gif(gif, overlay)
return "output.gif", "output.gif", "output.gif"
def front_static(img, obj_color, obj_color_picker, ai_num, txt_color, txt_color_picker, member_name_dropdown, group_name_radio, group_name_txt, obj_num, obj_alphabet, obj_alphabet_txt, obj_serial):
meta = PngImagePlugin.PngInfo()
meta.add_text('objektify', 'V1')
meta.add_text("obj_color", obj_color if obj_color is not None else "")
meta.add_text("obj_color_picker", obj_color_picker if obj_color_picker is not None else "")
meta.add_text("ai_num", str(ai_num) if str(ai_num) is not None else "")
meta.add_text("txt_color", txt_color if txt_color is not None else "")
meta.add_text("txt_color_picker", txt_color_picker if txt_color_picker is not None else "")
meta.add_text("member_name_dropdown", member_name_dropdown if member_name_dropdown is not None else "")
meta.add_text("group_name_radio", group_name_radio if group_name_radio is not None else "")
meta.add_text("group_name_txt", group_name_txt if group_name_txt is not None else "")
meta.add_text("obj_num", obj_num if obj_num is not None else "")
meta.add_text("obj_alphabet", obj_alphabet if obj_alphabet is not None else "")
meta.add_text("obj_alphabet_txt", obj_alphabet_txt if obj_alphabet_txt is not None else "")
meta.add_text("obj_serial", obj_serial if obj_serial is not None else "")
match obj_color:
case "Atom01":
obj_color = "#ffdd00"
front_side = edimg.color(side, obj_color)
img.paste(front_side, (1039, 104), front_side)
case "Binary01":
obj_color = "#00ff01"
front_side = edimg.color(side, obj_color)
img.paste(front_side, (1039, 104), front_side)
case "Cream01":
obj_color = "#ff7477"
front_side = edimg.color(side, obj_color)
img.paste(front_side, (1039, 104), front_side)
case "Divine01":
obj_color = "#a226f5"
front_side = edimg.color(side, obj_color)
img.paste(front_side, (1039, 104), front_side)
case "Special":
img.paste(side_special, (1039, 104), side_special)
case "OMA1":
img = edimg.paste_img(img, side_oma1, (1016, 83))
case "OMA2":
img = edimg.paste_img(img, side_oma2, (1016, 83))
case "Other color":
obj_color = obj_color_picker
front_side = edimg.color(side, obj_color)
img.paste(front_side, (1039, 104), front_side)
case "AI Color":
front_side = edimg.color_ai(ai_num)
img.paste(front_side, (1039, 0), front_side)
match txt_color:
case "White":
txt_color_r = 255
txt_color_g = 255
txt_color_b = 255
txt_color_hex = "#FFFFFF"
case "Black":
txt_color_r = 0
txt_color_g = 0
txt_color_b = 0
txt_color_hex = "#000000"
case _:
txt_color_r, txt_color_g, txt_color_b = fn.hex_to_rgb(txt_color_picker)
txt_color_hex = txt_color_picker
if member_name_dropdown:
img_name = edimg.txt(member_name_dropdown, txt_color_r, txt_color_g, txt_color_b, 61, 270, "l")
img.paste(img_name, (1050, 161), img_name)
match group_name_radio:
case "tripleS":
img_group_name = edimg.txt(group_name_radio, txt_color_r, txt_color_g, txt_color_b, 61, 270, "r")
img.paste(img_group_name, (1050, 1240), img_group_name)
case "ARTMS":
img_artms_side = edimg.color("resource/artms_side.png", txt_color_hex)
img.paste(img_artms_side, (1081, 1371), img_artms_side)
case "Other":
img_group_name = edimg.txt(group_name_txt, txt_color_r, txt_color_g, txt_color_b, 61, 270, "r")
img.paste(img_group_name, (1050, 1240), img_group_name)
group_name_radio = group_name_txt
case None:
pass
case _:
img_group_name = edimg.txt(group_name_radio, txt_color_r, txt_color_g, txt_color_b, 61, 270, "r")
img.paste(img_group_name, (1050, 1240), img_group_name)
if obj_num != "" and obj_alphabet != "":
if obj_alphabet == "Other":
obj_alphabet = obj_alphabet_txt
img_obj_num = edimg.txt(obj_num+obj_alphabet, txt_color_r, txt_color_g, txt_color_b, 75, 270, "r")
img.paste(img_obj_num, (1050, 452), img_obj_num)
if obj_serial:
img_serial = edimg.serial(obj_serial, txt_color_r, txt_color_g, txt_color_b, 75)
img.paste(img_serial, (1046, 871), img_serial)
img_copy = deepcopy(img)
img = edimg.rounded(img)
krtime = fn.get_kr_time()
img.save(f'/data/cache/objektify-{krtime}.png', pnginfo=meta)
return f'/data/cache/objektify-{krtime}.png', img_copy
sign_positions = {
"SeoYeon": (31, 1068),
"HyeRin": (0, 1103),
"JiWoo": (34, 1101),
"ChaeYeon": (1, 1053),
"YooYeon": (79, 1100),
"SooMin": (0, 1057),
"NaKyoung": (0, 1116),
"YuBin": (20, 1109),
"Kaede": (26, 1109),
"DaHyun": (42, 1104),
"Kotone": (31, 1105),
"YeonJi": (0, 1065),
"Nien": (58, 1108),
"SoHyun": (0, 1110),
"Xinyu": (0, 1105),
"Mayu": (49, 1056),
"Lynn": (61, 1105),
"JooBin": (84, 1118),
"HaYeon": (39, 1167),
"ShiOn": (170, 1105),
"ChaeWon": (64, 1124),
"Sullin": (41, 1146),
"SeoAh": (52, 1113),
"JiYeon": (65, 1129),
"HeeJin": (38, 1148),
"KimLip": (61, 1100),
"JinSoul": (52, 1113),
"Choerry": (46, 1022),
"HaSeul": (62, 1131)
}
cream01 = Image.open("resource/cream01.png")
atom01 = Image.open("resource/atom01.png")
binary01 = Image.open("resource/binary01.png")
divine01 = Image.open("resource/divine01.png")
back_ui = Image.open("resource/back_ui.png")
outline_ui = Image.open("resources/outline.png")
outline_ui_oma = Image.open("resources/outline_oma.png")
back_ = Image.open("resources/back.png")
right_modhaus = Image.open("resource/right_modhaus.png")
img_qr_white_space = Image.new("RGB", (335, 335), 'white')
def back(obj_color, obj_color_picker, ai_num, txt_color, txt_color_picker, group_name_radio, group_name_txt, member_name_dropdown, obj_num, obj_alphabet, obj_alphabet_txt, obj_serial, obj_outline_color, obj_outline_color_picker, logo_radio, class_radio, class_txt, season_radio, season_txt, season_txt_outline, sign, qr_url_dropdown, qr_Logo_radio, right_btn, preset, qr_caption):
match obj_color:
case 'OMA1':
frame = outline_ui_oma
case 'OMA2':
frame = outline_ui_oma
case _:
frame = outline_ui
match obj_outline_color:
case "White":
outline = edimg.color(frame, "#FFFFFF")
case "Black":
outline = edimg.color(frame, "#000000")
case _:
outline = edimg.color(frame, obj_outline_color_picker)
match obj_color:
case "Atom01":
obj_color = "#ffdd00"
img = edimg.color(back_, obj_color)
case "Binary01":
obj_color = "#00ff01"
img = edimg.color(back_, obj_color)
case "Cream01":
obj_color = "#ff7477"
img = edimg.color(back_, obj_color)
case "Divine01":
obj_color = "#a226f5"
img = edimg.color(back_, obj_color)
case "Special":
img = Image.open('resources/back_special.png.png')
case "OMA1":
img = Image.open('resources/back_oma1.png')
case "OMA2":
img = Image.open('resources/back_oma2.png')
case "Other color":
obj_color = obj_color_picker
img = edimg.color(back_, obj_color)
case "AI Color":
img = edimg.color_ai_back(ai_num)
case _:
img = edimg.color(back_, '#ffffff')
img.paste(outline, (0, 0), outline)
match txt_color:
case "White":
txt_color_r = 255
txt_color_g = 255
txt_color_b = 255
txt_color_hex = "#FFFFFF"
case "Black":
txt_color_r = 0
txt_color_g = 0
txt_color_b = 0
txt_color_hex = "#000000"
case _:
txt_color_r, txt_color_g, txt_color_b = fn.hex_to_rgb(txt_color_picker)
txt_color_hex = txt_color_picker
back_ui_colored = edimg.color(back_ui, txt_color_hex)
img.paste(back_ui_colored, (0, 0), back_ui_colored)
match logo_radio:
case "tripleS":
logo = edimg.color('resource/triples_logo.png', txt_color_hex)
img.paste(logo, (61, 162), logo)
case "ARTMS":
logo = edimg.color('resource/artms_logo.png', txt_color_hex)
img.paste(logo, (61, 162), logo)
case 'None':
pass
case _:
logo = edimg.color(f'/data/presets/{preset}/top_logo.png', txt_color_hex)
width, height = logo.size
# Calculate the new width to maintain the aspect ratio
new_height = 126
aspect_ratio = width / height
new_width = int(new_height * aspect_ratio)
# Resize the image
logo = logo.resize((new_width, new_height), Image.Resampling.LANCZOS)
img.paste(logo, (61, 162), logo)
if member_name_dropdown:
img_name = edimg.txt(member_name_dropdown, txt_color_r, txt_color_g, txt_color_b, 130, 0, "l", "font/Helvetica_Neue_LT_Std_65_Medium.otf", (800, 136))
img.paste(img_name, (54, 466), img_name)
img_name_side = edimg.txt(member_name_dropdown, txt_color_r, txt_color_g, txt_color_b, 61, 270, "l")
img.paste(img_name_side, (940, 159), img_name_side)
if obj_num:
if obj_alphabet == "Other":
obj_alphabet = obj_alphabet_txt
img_obj_num_side = edimg.txt(obj_num+obj_alphabet, txt_color_r,txt_color_g,txt_color_b, 75, 270, "r")
img.paste(img_obj_num_side, (951, 448), img_obj_num_side)
if obj_serial:
img_obj_serial_side = edimg.serial(obj_serial, txt_color_r,txt_color_g,txt_color_b, 75)
img.paste(img_obj_serial_side, (943, 867), img_obj_serial_side)
match group_name_radio:
case "tripleS":
img_group_name_side = edimg.txt("tripleS", txt_color_r, txt_color_g, txt_color_b, 61, 270, "r")
img.paste(img_group_name_side, (943, 1238), img_group_name_side)
case "ARTMS":
img_group_name_side = edimg.color("resource/artms_side.png", txt_color_hex)
img.paste(img_group_name_side, (974, 1371), img_group_name_side)
case "Other":
img_group_name_side = edimg.txt(group_name_txt, txt_color_r, txt_color_g, txt_color_b, 61, 270, "r")
img.paste(img_group_name_side, (943, 1238), img_group_name_side)
group_name_radio = group_name_txt
case None:
pass
case _:
img_group_name_side = edimg.txt(group_name_radio, txt_color_r, txt_color_g, txt_color_b, 61, 270, "r")
img.paste(img_group_name_side, (943, 1238), img_group_name_side)
group_name_radio = group_name_txt
match class_radio:
case "Zero" | "First" | "Double" | "Special":
img_class = edimg.txt(class_radio, txt_color_r, txt_color_g, txt_color_b, 130, 0, "l", "font/Helvetica_Neue_LT_Std_65_Medium.otf", (800, 136))
img.paste(img_class, (54, 700), img_class)
case "Other":
img_class = edimg.txt(class_txt, txt_color_r, txt_color_g, txt_color_b, 130, 0, "l", "font/Helvetica_Neue_LT_Std_65_Medium.otf", (800, 136))
img.paste(img_class, (54, 700), img_class)
match season_radio:
case "Atom01":
img_atom01 = edimg.color(atom01, txt_color_hex)
img.paste(img_atom01, (63, 951), img_atom01)
case "Binary01":
img_binary01 = edimg.color(binary01, txt_color_hex)
img.paste(img_binary01, (63, 951), img_binary01)
case "Cream01":
img_cream01 = edimg.color(cream01, txt_color_hex)
img.paste(img_cream01, (62, 950), img_cream01)
case "Divine01":
img_divine01 = edimg.color(divine01, txt_color_hex)
img.paste(img_divine01, (61, 951), img_divine01)
case "Other":
img_season = edimg.txt(season_txt, txt_color_r, txt_color_g, txt_color_b, 130, 0, "l", "font/Helvetica_Neue_LT_Std_65_Medium.otf", (1100, 136))
x = edimg.calculate_size(season_txt, 130, "font/Helvetica_Neue_LT_Std_65_Medium.otf")
img_season_outline = edimg.txt(season_txt_outline, txt_color_r, txt_color_g, txt_color_b, 135, 0, "l", "font/Helvetica_Neue_LT_Std_75_Bold_Outline.otf", (1100, 136))
img.paste(img_season,(53, 936), img_season)
img.paste(img_season_outline, (53 + x, 936), img_season_outline)
if sign in pre.names:
colored_icon = edimg.color(f'sign/{sign}.png', txt_color_hex)
position = sign_positions[sign]
img.paste(colored_icon, position, colored_icon)
elif sign:
colored_icon = edimg.color(f'/data/presets/{preset}/{sign}.png', txt_color_hex)
original_width, original_height = colored_icon.size
ratio = min(480 / original_width, 330 / original_height)
new_width = int(original_width * ratio)
new_height = int(original_height * ratio)
colored_icon = colored_icon.resize((new_width, new_height), Image.Resampling.LANCZOS)
img = maker.space(img, (63, 1097), (488, 335), colored_icon)
if qr_url_dropdown:
url_txt = fn.url_mapping(qr_url_dropdown)
img_qr = edimg.qr(url_txt)
match qr_Logo_radio:
case "tripleS":
img_qr_icon = edimg.qr_icon("resource/qr_icon_triples.png")
img_qr.paste(img_qr_icon, (126, 126), img_qr_icon)
case 'None':
pass
case _:
img_qr_icon = Image.open(f'/data/presets/{preset}/qr_logo.png')
colored_icon = img_qr_icon
original_width, original_height = colored_icon.size
ratio = min(84 / original_width, 84 / original_height)
new_width = int(original_width * ratio)
new_height = int(original_height * ratio)
colored_icon = colored_icon.resize((new_width, new_height), Image.Resampling.LANCZOS)
qr_logo_bg = Image.new(size=(84, 84), mode='RGB', color=(255,255,255))
qr_logo_bg = maker.space(qr_logo_bg, (0,0), (84,84), colored_icon)
img_qr.paste(qr_logo_bg, (126, 126))
img_qr = edimg.resize_img(img_qr, 335,335)
img.paste(img_qr, (555, 1098))
else:
img.paste(img_qr_white_space,(555, 1098))
if qr_caption:
qr_caption_img = maker.draw(36, qr_caption, color=txt_color_hex)
img = maker.space(img, (553, 1453), (339,37), qr_caption_img)
if right_btn:
img_right_modhaus = edimg.color(right_modhaus, txt_color_hex)
img.paste(img_right_modhaus, (61,1604), img_right_modhaus)
krtime = fn.get_kr_time()
img_jpeg = img.convert('RGB')
img_jpeg.save(f'/data/cache/{member_name_dropdown}_{group_name_radio}_{class_radio}_{obj_num}{obj_alphabet}_{krtime}.jpg', quality=90)
return img, img
make_image_first = Image.open("resource/make_image_first.png")
remove_black = Image.open("resource/remove_black.png")
def ex_gen(img_front, file_front_path, img_back, count, blur, blur_strength, watermark):
if img_back == None:
gr.Warning("Make two images, front and back")
return make_image_first, make_image_first
if img_front == None and file_front_path == None:
gr.Warning("Make two images, front and back")
return make_image_first, make_image_first
if file_front_path:
file_front = Image.open(file_front_path)
match count:
case "One File":
img_back = img_back.resize(file_front.size)
img_remove_black = remove_black.resize(file_front.size)
img_back.paste(img_remove_black,(0,0), img_remove_black)
filename, _ = os.path.splitext(os.path.basename(file_front_path))
edimg.makegif(file_front, img_back)
return f"exgif.gif", img_back
case "Two Files":
return file_front, img_back
if blur:
img_front = img_front.filter(ImageFilter.GaussianBlur(blur_strength))
img_back = img_back.filter(ImageFilter.GaussianBlur(blur_strength))
if watermark:
img_txt = edimg.txt(watermark, 255, 255, 255, 210, 50, "l", "font/Helvetica_Neue_LT_Std_75_Bold.otf",(1338, 222), 120)
img_front.paste(img_txt, (65, 333), img_txt)
img_back.paste(img_txt, (65, 333), img_txt)
match count:
case "One File":
img = Image.new("RGBA", (2328, 1800), (0, 0, 0, 0))
img_front = edimg.rounded(img_front)
img_back = edimg.rounded(img_back)
img.paste(img_front, (0, 0), img_front)
img.paste(img_back, (1164, 0), img_back)
return img, img_blank
case "Two Files":
img_front = edimg.rounded(img_front)
img_back = edimg.rounded(img_back)
return img_front, img_back
def find_value(n, percent):
return n * (percent / 100.0)