Spaces:
Runtime error
Runtime error
File size: 6,269 Bytes
17d49e3 e95f482 17d49e3 7e5462a efd2c2f 17d49e3 1246cc0 431b076 72dff7c 431b076 7e5462a 1246cc0 9062df6 1246cc0 9062df6 1246cc0 9062df6 1246cc0 9062df6 1246cc0 9062df6 1246cc0 17d49e3 9062df6 e95f482 17d49e3 d538b9e 17d49e3 d538b9e 17d49e3 e95f482 64d00f9 e95f482 d538b9e efd2c2f e95f482 d1f3dc2 17d49e3 d1f3dc2 17d49e3 efd2c2f 17d49e3 efd2c2f 17d49e3 efd2c2f 17d49e3 efd2c2f 17d49e3 efd2c2f 17d49e3 d1f3dc2 1246cc0 |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
from PIL import Image, ImageOps, PngImagePlugin
import random as rn
import gradio as gr
from datetime import datetime
import pytz
#from interface import all_components
from text_assist import *
season_color = {'Atom01': '#FFDD00', 'Binary01': '#00FF00', 'Cream01': '#FF7477', 'Divine01': '#B301FE','Ever01': '#33ecfd'}
#all_components = [common_name, common_group, quick_season, balanced_season, balanced_class, balanced_number, balanced_line, balanced_serial]
mask = Image.open('front_resource/mask.png')
import os
# ν΄λ κ²½λ‘ μ§μ
folder_path = "./data/cache"
# ν΄λ μμ± (μ‘΄μ¬νμ§ μλ κ²½μ°)
os.makedirs(folder_path, exist_ok=True)
def image_uploaded(image_path):
image = Image.open(image_path)
try:
image = ImageOps.exif_transpose(image) #https://github.com/python-pillow/Pillow/issues/4703
except ZeroDivisionError:
image = image.rotate(270, expand=True) #There is an issue with vertically taken photos in the Kiwi Browser on Android, so I manually rotate them.
base = Image.new('RGBA', mask.size, (0, 0, 0, 0))
if not image.size == mask.size:
blank_image = Image.new('RGBA', mask.size, (0, 0, 0, 0))
x_pos = 0
y_pos = 0
r = mask.size[0]/image.size[0]
y_r = image.size[1] * r
x_r = image.size[0] * r
if y_r > mask.size[1]:
y_pos = (mask.size[1]-y_r)//2
image = image.resize((int(x_r), int(y_r)), Image.Resampling.LANCZOS)
elif y_r < mask.size[1]:
r = mask.size[1]/image.size[1]
y_r = image.size[1] * r
x_r = image.size[0] * r
x_pos = (mask.size[0]-x_r)//2
image = image.resize((int(x_r), int(y_r)), Image.Resampling.LANCZOS)
elif y_r == mask.size[1]:
image = image.resize(mask.size, Image.Resampling.LANCZOS)
x_pos = 0
y_pos = 0
blank_image.paste(image, (int(x_pos), int(y_pos)))
base.paste(blank_image, (0,0), mask)
else:
base.paste(image, (0,0), mask)
return base
if __name__ == '__main__':
image = Image.new('RGBA', (200,100), (255,255,255,255))
image_uploaded(image).show()
def front(mode: str , image: Image, *args):
if not image:
# Remove black screen
# image = Image.new(size=(1083, 1673), mode='RGBA', color=(0,0,0,255))
return None, None
else:
image.convert('RGBA')
color = [(255, 255, 255), (0, 0, 0)]
number = '100'
line = 'Z'
serial = '0'
if mode == 'quick':
color = (season_color[args[2]], (0,0,0))
number = f"{rn.randint(101, 108)}"
line = 'Z'
serial = rn. randint(1, 1800)
if mode == 'balanced':
match args[4]:
case 'First':
color = (season_color[args[3]], (0,0,0))
case 'Welcome':
color = ((255,255,255), (0,0,0))
case 'Special':
color = ('Special', (0,0,0))
case 'Premier':
color = ('#2E3192', (255,255,255))
case 'OMA1':
color = ('OMA1', (255,255,255))
case 'OMA2':
color = ('OMA2', (255,255,255))
number = (str(args[5]) if args[5] else None)
line = args[6] if args[6] else ''
serial = args[7] if args[7] else None
if mode == 'expert':
print(args[9], args[12])
match args[8]:
case 'Static':
color = (colorPicker2rgb(str(args[9])), colorPicker2rgb(str(args[12])))
case 'Gradient':
pass
case 'Image':
pass
image = generate_front(image, color, args[0], args[1], number, line, serial)
meta = PngImagePlugin.PngInfo()
meta.add_text('objektify', 'V3')
krtime = get_kr_time()
image.save(f'{folder_path}/objektify-{krtime}.png', pnginfo=meta) # save to cache
return image, f'{folder_path}/objektify-{krtime}.png'
def colorPicker2rgb(value: str):
if value.startswith('rgba'):
# "rgba(10,20,20,1)" νμμμ μ«μλ§ μΆμΆ
rgba_values = value.strip("rgba()").split(",")
# RGB κ°μ λΆλμμμ μμ μ μλ‘ λ³ν
rgb_tuple = tuple(map(lambda x: round(float(x)), rgba_values[:3]))
return rgb_tuple
elif value.startswith('#'):
return hex2rgb(value)
def get_kr_time():
# νκ΅ μκ°λ μ€μ
korea_time_zone = pytz.timezone('Asia/Seoul')
# νμ¬ μκ°μ νκ΅ μκ°λλ‘ λ³ν
current_time_in_korea = datetime.now(korea_time_zone)
# μνλ νμμΌλ‘ μκ° ν¬λ§·ν
return current_time_in_korea.strftime("%Y%m%d-%H%M%S")
side_mask = Image.open('front_resource/side.png')
def generate_front(image, color: list, name, group, number, line, serial): #color = (main_color, text_color)
if not isinstance(color[0], tuple) and not color[0].startswith('#'):
side_bar = Image.open(f'front_resource/{color[0]}.png')
else:
side_bar = Image.new('RGBA', side_mask.size, color[0])
draw = ImageDraw.Draw(side_bar)
if name:
text_draw_default(draw, (51, 35), "Helvetica_Neue_LT_Std_75_Bold.otf", 60, name, color[1])
if group:
text_draw_right(draw, (1428, 35), "Helvetica_Neue_LT_Std_75_Bold.otf", 60, group, color[1])
if number and serial:
text_draw_right(draw, (688+30, 35), "Helvetica_Neue_LT_Std_75_Bold.otf", 68, str(number)+line,color[1])
text_draw_default(draw, (706+30, 30), "MatrixSSK_custom.ttf", 68, '#'+str(serial).zfill(5),color[1])
if number and not serial:
text_draw_center(draw, (side_mask.size[0]//2 + 7, side_mask.size[1]//2), "Helvetica_Neue_LT_Std_75_Bold.otf", 68, str(number)+line,color[1])
if serial and not number:
text_draw_center(draw, (side_mask.size[0]//2, side_mask.size[1]//2 -5), "MatrixSSK_custom.ttf", 68, '#'+str(serial).zfill(5),color[1])
if isinstance(color[0], str) and color[0].startswith('OMA'):
additional_bar = Image.open(f'front_resource/{color[0]}_add.png')
image.paste(additional_bar, (944, 77), additional_bar)
image.paste(side_bar.rotate(270, expand=True), (965, 98), side_mask.rotate(270, expand=True))
print('generated!')
return image |