import os import time import numpy as np import gradio as gr import fn import edimg import edgif from PIL import Image, ImageDraw, ImageFont, ImageColor, ImageFilter, ImageSequence img_blank = Image.open('resource/blank.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, rounded): 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 img = 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, rounded) return img, img, None 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, rounded) gif = Image.open(img_dynamic_file) edgif.attach_gif(gif, overlay, rounded) 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, rounded): match obj_color: case "Atom01": obj_color = "#ffdd00" front_side = edimg.color("resource/front_side.png", obj_color) img.paste(front_side, (0, 0), front_side) case "Binary01": obj_color = "#00ff01" front_side = edimg.color("resource/front_side.png", obj_color) img.paste(front_side, (0, 0), front_side) case "Cream01": obj_color = "#ff7477" front_side = edimg.color("resource/front_side.png", obj_color) img.paste(front_side, (0, 0), front_side) case "Divine01": obj_color = "#a226f5" front_side = edimg.color("resource/front_side.png", obj_color) img.paste(front_side, (0, 0), front_side) case "Special": img = edimg.paste_img(img, "resource/special_side.png", (0, 0)) case "OMA": img = edimg.paste_img(img, "resource/oma_side.png", (1016, 83)) case "Other color": obj_color = obj_color_picker front_side = edimg.color("resource/front_side.png", obj_color) img.paste(front_side, (0, 0), front_side) case "AI Color": front_side = edimg.color_ai(ai_num) img.paste(front_side, (1038, 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) 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) if rounded: img = edimg.rounded(img) krtime = fn.get_kr_time() img_jpeg = img.convert('RGB') img_jpeg.save(f'/data/cache/{member_name_dropdown}_{group_name_radio}_{obj_num}{obj_alphabet}_{krtime}.jpg', quality=90) return img back_ui = Image.open("resource/back_ui.png") outline_ui = Image.open("resource/outline.png") sign_positions = { 1: (31, 1068), 2: (0, 1103), 3: (34, 1101), 4: (1, 1053), 5: (79, 1100), 6: (0, 1057), 7: (0, 1116), 8: (20, 1109), 9: (26, 1109), 10: (42, 1104), 11: (31, 1105), 12: (0, 1065), 13: (58, 1108), 14: (0, 1110), 15: (0, 1105), 16: (49, 1056), 17: (61, 1105), 18: (84, 1118), 19: (39, 1167), 20: (170, 1105), 21: (64, 1124), 22: (41, 1146), 23: (52, 1113), 24: (65, 1129), 25: (38, 1148), 26: (61, 1100), 27: (52, 1113), 28: (46, 1022), 29: (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") oma_frame = Image.open("resource/oma_frame.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, rounded_back): match obj_outline_color: case "White": outline = edimg.color(outline_ui, "#FFFFFF") case "Black": outline = edimg.color(outline_ui, "#000000") case _: outline = edimg.color(outline_ui, obj_outline_color_picker) match obj_color: case "Atom01": obj_color = "#ffdd00" img = Image.new("RGB", (1164, 1800), obj_color) case "Binary01": obj_color = "#00ff01" img = Image.new("RGB", (1164, 1800), obj_color) case "Cream01": obj_color = "#ff7477" img = Image.new("RGB", (1164, 1800), obj_color) case "Divine01": obj_color = "#a226f5" img = Image.new("RGB", (1164, 1800), obj_color) case "Special": img = Image.open('resource/special_back.png') case "OMA": img = Image.open('resource/oma_back.png') case "Other color": obj_color = obj_color_picker img = Image.new("RGB", (1164, 1800), obj_color) case "AI Color": img = edimg.color_ai_back(ai_num) case _: img = Image.new("RGB", (1164, 1800), "#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) 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, (932, 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 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 != None: sign = sign + 1 colored_icon = edimg.color(f'sign/{sign}.png', txt_color_hex) if sign in sign_positions: position = sign_positions[sign] img.paste(colored_icon, position, 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) 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 right_btn: img_right_modhaus = edimg.color(right_modhaus, txt_color_hex) img.paste(img_right_modhaus, (61,1604), img_right_modhaus) if obj_color == "OMA": img.paste(oma_frame, (0, 0), oma_frame) if rounded_back: img = edimg.rounded(img) 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 underbar = Image.open("event/under.png") def event(event_img, event_radio, event_txt, position): if event_img == None: gr.Warning("Upload the image first") return "resource/upload_first.png" img_jpeg = event_img.convert('RGB') krtime = fn.get_kr_time() img_jpeg.save( f'/data/cache/event_original_{krtime}.jpg', quality=90) #txt가 x700 넘을때 경우 나누기 images_list = [] # 반복문을 사용하여 이미지 생성 및 추가 for i in range(4): event_img_s = event_img.copy() event_black = Image.open("event/Black.png") event_white = Image.open("event/White.png") match event_radio: case "White": r = 255 g = 255 b = 255 hex = "#FFFFFF" event_img_gnd = event_white case "Black": r = 0 g = 0 b = 0 hex = "#000" event_img_gnd = event_black case _: r = 0 g = 0 b = 0 hex = "#000" event_img_gnd = event_black i1 = i + 1 x = edimg.calculate_size(event_txt, 260, f"event/{i1}.ttf") match i: case 0: offset_x = -30 offset_y = 0 case 1: offset_x = 80 offset_y = 0 case 2: offset_x = 0 offset_y = 0 case 3: offset_x = 70 offset_y = 20 case _: offset_x = 0 offset_y = 0 img_txt = edimg.txt(event_txt, r, g, b, 260, 0, "l", f"event/{i1}.ttf", (3000, 370 - offset_y)) x_half = x / 2 event_img_gnd.paste(img_txt, (2434 - int(x_half), 0), img_txt) x, y = event_img_s.size x1, y1 = event_img_gnd.size y2 = x * y1 / x1 event_img_gnd = edimg.resize_img(event_img_gnd, int(x), int(y2)) y3 = find_value(y - y2, position) event_img_s.paste(event_img_gnd, (0, int(y3)), event_img_gnd) img_jpeg = event_img_s.convert('RGB') krtime = fn.get_kr_time() img_jpeg.save( f'/data/cache/event_{krtime}_{i1}.jpg', quality=70) # 이미지 리스트에 추가 images_list.append(event_img_s) return images_list def find_value(n, percent): return n * (percent / 100.0)