P01yH3dr0n commited on
Commit
5893438
1 Parent(s): beeb56d

Create images_history.py

Browse files
Files changed (1) hide show
  1. images_history.py +64 -0
images_history.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import toml
4
+ from math import ceil
5
+ from pnginfo import read_info_from_image
6
+
7
+ client_config = toml.load("config.toml")['client']
8
+ num_of_imgs_per_page = 60
9
+
10
+ def get_img_list(folder, page):
11
+ img_list = [os.path.join(client_config['save_path'], folder, f) for f in os.listdir(os.path.join(client_config['save_path'], folder))]
12
+ img_list.sort(key=lambda f: os.path.getctime(f), reverse=True)
13
+ if page == 0:
14
+ page = 1
15
+ elif page*num_of_imgs_per_page > len(img_list) or page < 0:
16
+ page = ceil(len(img_list)/num_of_imgs_per_page)
17
+ res = img_list[(page - 1)*num_of_imgs_per_page: page*num_of_imgs_per_page]
18
+ return gr.Gallery(value=res, selected_index=None), page
19
+
20
+ def update_img_dir():
21
+ all_dirs = os.listdir(client_config['save_path'])
22
+ all_dirs.sort(reverse=True)
23
+ img_dir_path = gr.update(choices=all_dirs)
24
+ return img_dir_path
25
+
26
+ def get_img_info(evt: gr.SelectData):
27
+ info, items = read_info_from_image(evt.value['image']['path'])
28
+ index = evt.index
29
+ return index, info, items
30
+
31
+ def img_history_ui(tab):
32
+ all_dirs = os.listdir(client_config['save_path'])
33
+ all_dirs.sort(reverse=True)
34
+ with gr.Row():
35
+ img_dir_path = gr.Dropdown(choices=all_dirs, value=None, label='图片文件夹')
36
+
37
+ tab.select(fn=update_img_dir, inputs=None, outputs=img_dir_path)
38
+ with gr.Row():
39
+ with gr.Column(scale=2):
40
+ with gr.Row():
41
+ first_page = gr.Button('第一页')
42
+ prev_page = gr.Button('前一页')
43
+ page_index = gr.Number(value=1, label="页码", precision=0)
44
+ next_page = gr.Button('下一页')
45
+ end_page = gr.Button('末尾页')
46
+ with gr.Row():
47
+ history_gallery = gr.Gallery(show_label=False, columns=6)
48
+ with gr.Column(scale=1):
49
+ info = gr.HTML()
50
+ items = gr.JSON(value=None, visible=False)
51
+ selected_index = gr.Number(value=-1, visible=False, precision=0)
52
+ with gr.Row():
53
+ send_btn = gr.Button('参数发送到文生图')
54
+
55
+ img_dir_path.change(lambda path: get_img_list(path, 1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items])
56
+ img_dir_path.blur(update_img_dir, inputs=None, outputs=img_dir_path)
57
+ first_page.click(lambda path: get_img_list(path, 1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items])
58
+ prev_page.click(lambda path, p: get_img_list(path, p - 1) + ('', None), inputs=[img_dir_path, page_index], outputs=[history_gallery, page_index, info, items])
59
+ page_index.submit(lambda path, p: get_img_list(path, p) + ('', None), inputs=[img_dir_path, page_index], outputs=[history_gallery, page_index, info, items])
60
+ next_page.click(lambda path, p: get_img_list(path, p + 1) + ('', None), inputs=[img_dir_path, page_index], outputs=[history_gallery, page_index, info, items])
61
+ end_page.click(lambda path: get_img_list(path, -1) + ('', None), inputs=img_dir_path, outputs=[history_gallery, page_index, info, items])
62
+ history_gallery.select(get_img_info, inputs=None, outputs=[selected_index, info, items])
63
+
64
+ return send_btn, items