P01yH3dr0n commited on
Commit
522de11
1 Parent(s): 43ef58c

img rename save test

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -3,7 +3,7 @@ import datetime
3
 
4
  import toml
5
  import gradio as gr
6
- from PIL import Image, PngImagePlugin
7
  from huggingface_hub import HfApi, snapshot_download
8
 
9
  from pnginfo import read_info_from_image, send_paras
@@ -97,7 +97,7 @@ def control_ui():
97
  inp.select(lambda: 'inp', inputs=None, outputs=selection)
98
  return gen_btn, [prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_image, info_extract, ref_str, i2i_image, i2i_str, i2i_noise, overlay, inp_img, selection], [save, rand_seed, reuse_seed, reuse_img_vibe, reuse_img_i2i, reuse_img_inp]
99
 
100
- def generate(prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_image, info_extract, ref_str, i2i_image, i2i_str, i2i_noise, overlay, inp_img, selection, save):
101
  global today_count
102
  set_token(os.environ.get('token'))
103
  img_data, payload = generate_novelai_image(
@@ -110,17 +110,6 @@ def generate(prompt, quality_tags, neg_prompt, seed, scale, width, height, steps
110
  return gr.Image(value=None), payload
111
  today_count = get_count() + 1
112
  img = image_from_bytes(img_data)
113
- if save:
114
- save_path = client_config['save_path']
115
- today = datetime.date.today().strftime('%Y-%m-%d')
116
- today_path = os.path.join(save_path, today)
117
- os.makedirs(today_path, exist_ok=True)
118
- filename = str(today_count).rjust(5, '0') + '-' + str(payload['parameters']['seed']) + '.png'
119
- pnginfo_data = PngImagePlugin.PngInfo()
120
- for k, v in img.info.items():
121
- pnginfo_data.add_text(k, str(v))
122
- img.save(os.path.join(today_path, filename), pnginfo=pnginfo_data)
123
- api.upload_folder(folder_path=today_path, path_in_repo=today_path, repo_id="P01yH3dr0n/naimages", repo_type="dataset", token=os.environ.get("hf_token"))
124
  return img, payload
125
 
126
  def preview_ui():
@@ -129,6 +118,22 @@ def preview_ui():
129
  info = gr.JSON(value={}, label="生成信息")
130
  return image, info
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  def main_ui():
133
  with gr.Blocks() as page:
134
  with gr.Row(variant="panel"):
@@ -136,11 +141,12 @@ def main_ui():
136
  gen_btn, paras, others = control_ui()
137
  with gr.Column():
138
  image, info = preview_ui()
139
- gen_btn.click(generate, paras + [others[0]], [image, info], concurrency_limit=1)
140
  others[2].click(lambda o, s: o if len(s) == 0 else s['parameters']['seed'], inputs=[paras[3], info], outputs=paras[3])
141
  others[3].click(lambda i: i, inputs=image, outputs=paras[14])
142
  others[4].click(lambda i: i, inputs=image, outputs=paras[17])
143
- others[5].click(lambda i: gr.ImageEditor(value={'background': i, 'layers': [Image.new('RGBA', i.size)], 'composite': i}), inputs=image, outputs=paras[21])
 
144
  return page, paras[:14]
145
 
146
  def util_ui():
 
3
 
4
  import toml
5
  import gradio as gr
6
+ import shutil
7
  from huggingface_hub import HfApi, snapshot_download
8
 
9
  from pnginfo import read_info_from_image, send_paras
 
97
  inp.select(lambda: 'inp', inputs=None, outputs=selection)
98
  return gen_btn, [prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_image, info_extract, ref_str, i2i_image, i2i_str, i2i_noise, overlay, inp_img, selection], [save, rand_seed, reuse_seed, reuse_img_vibe, reuse_img_i2i, reuse_img_inp]
99
 
100
+ def generate(prompt, quality_tags, neg_prompt, seed, scale, width, height, steps, sampler, scheduler, smea, dyn, dyn_threshold, cfg_rescale, ref_image, info_extract, ref_str, i2i_image, i2i_str, i2i_noise, overlay, inp_img, selection):
101
  global today_count
102
  set_token(os.environ.get('token'))
103
  img_data, payload = generate_novelai_image(
 
110
  return gr.Image(value=None), payload
111
  today_count = get_count() + 1
112
  img = image_from_bytes(img_data)
 
 
 
 
 
 
 
 
 
 
 
113
  return img, payload
114
 
115
  def preview_ui():
 
118
  info = gr.JSON(value={}, label="生成信息")
119
  return image, info
120
 
121
+ def rename_save_img(path, payload, save):
122
+ if path is None:
123
+ return None
124
+ default = os.path.basename(path)
125
+ filename = str(today_count).rjust(5, '0') + '-' + str(payload['parameters']['seed']) + '.png'
126
+ renamed_path = path.replace(default, filename)
127
+ os.replace(path, renamed_path)
128
+ if save:
129
+ save_path = client_config['save_path']
130
+ today = datetime.date.today().strftime('%Y-%m-%d')
131
+ today_path = os.path.join(save_path, today)
132
+ os.makedirs(today_path, exist_ok=True)
133
+ shutil.copy(renamed_path, os.path.join(today_path, filename))
134
+ api.upload_folder(folder_path=today_path, path_in_repo=today_path, repo_id="P01yH3dr0n/naimages", repo_type="dataset", token=os.environ.get("hf_token"))
135
+ return renamed_path
136
+
137
  def main_ui():
138
  with gr.Blocks() as page:
139
  with gr.Row(variant="panel"):
 
141
  gen_btn, paras, others = control_ui()
142
  with gr.Column():
143
  image, info = preview_ui()
144
+ gen_btn.click(generate, paras, [image, info], concurrency_limit=1)
145
  others[2].click(lambda o, s: o if len(s) == 0 else s['parameters']['seed'], inputs=[paras[3], info], outputs=paras[3])
146
  others[3].click(lambda i: i, inputs=image, outputs=paras[14])
147
  others[4].click(lambda i: i, inputs=image, outputs=paras[17])
148
+ others[5].click(lambda i: gr.ImageEditor(value=i), inputs=image, outputs=paras[21])
149
+ image.change(rename_save_img, inputs=[image, info, others[0]], outputs=image)
150
  return page, paras[:14]
151
 
152
  def util_ui():