objektify2 / preset_fn.py
tripleS-Dev
update V1.3.3L Beta
eb6093b
raw
history blame
4.37 kB
import cairosvg
import gradio as gr
import random
import shutil
import fn
import edimg
import os
from PIL import Image, ImageDraw, ImageFont, ImageColor, ImageFilter, ImageSequence
import pickle
import pre
def refresh():
directory_path = "/data/presets"
if not os.path.exists(directory_path):
os.makedirs(directory_path)
else:
print(f"폴더 '{directory_path}'가 이미 존재합니다.")
items = os.listdir(directory_path)
folders = [item for item in items]
return gr.Dropdown(folders)
def refresh_folder():
directory_path = "/data/presets"
if not os.path.exists(directory_path):
os.makedirs(directory_path)
else:
print(f"폴더 '{directory_path}'가 이미 존재합니다.")
items = os.listdir(directory_path)
folders = [item for item in items]
return folders
def create(userid, user_name, preset_name, group, artist, company, top_logo, qr_logo, sign_img1, sign_img2, sign_img3, sign_img4, sign_img5, sign_img6, sign_img7, sign_img8, sign_img9, sign_img10, sign_img11, sign_img12):
print(user_name)
path = f"/data/presets/{preset_name}_{user_name.split('#')[0]}"
if not os.path.exists(path):
os.makedirs(path)
else:
shutil.rmtree(path)
os.makedirs(path)
if isinstance(top_logo, str):
cairosvg.svg2png(url=top_logo, write_to=f"{path}/top_logo.png")
else:
top_logo.save(f"{path}/top_logo.png")
if isinstance(qr_logo, str):
cairosvg.svg2png(url=qr_logo, write_to=f"{path}/qr_logo.png")
else:
qr_logo.convert('RGBA').save(f"{path}/qr_logo.png")
signs = [sign_img1, sign_img2, sign_img3, sign_img4, sign_img5, sign_img6, sign_img7, sign_img8, sign_img9, sign_img10, sign_img11, sign_img12]
for i in range(len(artist)):
if isinstance(signs[i], str):
cairosvg.svg2png(url=signs[i], write_to=f"{path}/{artist[i]}.png")
else:
signs[i].save(f"{path}/{artist[i]}.png")
data = {}
data['userid'] = userid
data['preset_name'] = preset_name
data['group'] = group
data['artist'] = artist
data['company'] = company
with open(f'{path}/data.pkl', 'wb') as f:
pickle.dump(data, f)
f.close()
return f"{preset_name}_{user_name.split('#')[0]}"
def apply(folder):
if folder == None:
return
directory_path = f"/data/presets/{folder}"
with open(f'{directory_path}/data.pkl', 'rb') as f:
data = pickle.load(f)
group_name = data['group']
artist_name = data['artist']
group_name_radio = gr.Radio(choices=[group_name] + pre.groups_Other, label="Group Name", value=group_name)
member_name_dropdown = gr.Dropdown(choices=artist_name, label="Member Name", allow_custom_value=True,
value=artist_name[0])
logo_radio = gr.Radio(choices=[group_name] + pre.groups, label="Add Logo", interactive=True, value=group_name)
sign = gr.Dropdown(choices=artist_name + pre.names, label="Sign", value=artist_name[0])
qr_Logo_radio = gr.Radio([group_name] + ["tripleS", "None"], label="QR Logo", value=group_name, interactive=True)
preset = gr.Dropdown()
return group_name_radio, member_name_dropdown, logo_radio, sign, qr_Logo_radio, preset
import zipfile
import os
def unzip_file_to_directory(zip_file_path):
shutil.rmtree('/data/presets')
output_directory = '/data/presets/'
# Ensure the output directory exists
os.makedirs(output_directory, exist_ok=True)
# Unzip the file
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(output_directory)
print(f"Unzipped {zip_file_path} to {output_directory}")
import zipfile
import os
def zip_directory():
# Example usage
directory_path = '/data/presets/'
zip_file_path = '/data/presets.zip'
# Create a zip file
with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
# Walk through the directory
for root, dirs, files in os.walk(directory_path):
for file in files:
# Create the full filepath
full_path = os.path.join(root, file)
# Add the file to the zip file
zipf.write(full_path, os.path.relpath(full_path, directory_path))
print(f"Compressed {directory_path} to {zip_file_path}")