add uploaded images as layers

This commit is contained in:
Aedh Carrick 2022-12-05 11:47:59 -06:00
parent 2a8366074b
commit 064a7e9554
3 changed files with 98 additions and 46 deletions

View File

@ -6,6 +6,7 @@ from scripts import flet_utils
class LayerManager(ft.Container):
# first make a column to hold the layers
def make_layer_holder(self):
layer_holder = ft.DragTarget(
group = 'layer',
@ -19,7 +20,8 @@ class LayerManager(ft.Container):
on_leave = self.layer_leave,
)
return layer_holder
# make a slot for each layer to go in
def make_layer_slot(self):
layer_slot = LayerSlot(
group = 'layer',
@ -38,6 +40,7 @@ class LayerManager(ft.Container):
)
return layer_slot
# make the display for the layer
def make_layer_display(self):
try:
self.layer_count += 1
@ -93,14 +96,20 @@ class LayerManager(ft.Container):
layer_display.controls[1].content.controls.extend([layer_icon,layer_label,layer_handle])
return layer_display
def update_layer_indexes(self):
layer_list = self.data['layer_list']
index = 0
for layer in layer_list:
if layer.data['type'] == 'slot':
layer.data['index'] = index
index += 1
# keep track of which layers are active/visible
def show_hide_layer(self, e):
parent = e.control.data['parent']
if parent.data['visible']:
parent.data['visible'] = False
parent.opacity = 0.5
e.control.icon = ft.icons.VISIBILITY_OFF
else:
parent.data['visible'] = True
parent.opacity = 1.0
e.control.icon = ft.icons.VISIBILITY
self.update_active_layer_list()
parent.update()
def update_active_layer_list(self):
self.data['active_layer_list'] = []
layer_list = self.data['layer_list']
@ -109,6 +118,15 @@ class LayerManager(ft.Container):
if layer.content.content.controls[1].data['visible']:
self.data['active_layer_list'].append(layer)
# handle layer shuffling
def update_layer_indexes(self):
layer_list = self.data['layer_list']
index = 0
for layer in layer_list:
if layer.data['type'] == 'slot':
layer.data['index'] = index
index += 1
def move_layer_slot(self, index):
layer_list = self.data['layer_list']
self.data['layer_being_moved'] = layer_list.pop(index)
@ -128,22 +146,6 @@ class LayerManager(ft.Container):
self.update_active_layer_list()
self.update()
def show_hide_layer(self, e):
parent = e.control.data['parent']
if parent.data['visible']:
parent.data['visible'] = False
parent.opacity = 0.5
e.control.icon = ft.icons.VISIBILITY_OFF
else:
parent.data['visible'] = True
parent.opacity = 1.0
e.control.icon = ft.icons.VISIBILITY
self.update_active_layer_list()
parent.update()
def layer_right_click(self,e):
pass
def layer_slot_will_accept(self, e):
if not self.data['layer_being_moved']:
return
@ -168,7 +170,7 @@ class LayerManager(ft.Container):
return
self.move_layer_slot(index)
## tab controls
# catch layers that are dropped outside slots
def layer_will_accept(self, e):
if not self.data['layer_being_moved']:
return
@ -206,16 +208,7 @@ class LayerManager(ft.Container):
layer_list.pop(-1)
self.update_layers()
def add_images_as_layers(images):
layer_list = self.data['layer_list']
for img in images:
layer_slot = self.make_layer_slot()
self.set_layer_slot_name(layer_slot, img.name)
layer_slot.data['image'] = img.data
layer_list.append(layer_slot)
self.page.message(f'added "{img.name}" as layer')
self.update_layers()
# handle toolbar functions
def add_blank_layer(self, e):
layer_list = self.data['layer_list']
layer_slot = self.make_layer_slot()
@ -224,7 +217,24 @@ class LayerManager(ft.Container):
self.page.message("added blank layer to canvas")
self.update_layers()
def add_images_as_layers(self, images):
layer_list = self.data['layer_list']
for img in images:
if images[img] == None:
continue
layer_slot = self.make_layer_slot()
layer_slot.set_layer_slot_name(img)
layer_slot.data['image'] = images[img]
layer_list.append(layer_slot)
self.page.message(f'added "{img}" as layer')
self.update_layers()
# fetch layer option on right click
def layer_right_click(self,e):
pass
# make each layer slot a target
class LayerSlot(ft.DragTarget):
def set_layer_slot_name(self, name):
self.content.content.controls[1].content.controls[1].value = name

View File

@ -13,7 +13,7 @@ def log_message(message):
# get time and format message
prefix = datetime.now()
msg_prefix = prefix.strftime("%Y/%m/%d %H:%M:%S")
message = msg_prefix + message
message = msg_prefix + " " + message
# check to see if we're appending to current logfile or making a new one'
try:
log_file = log_message.log
@ -60,10 +60,21 @@ def save_user_settings_to_config(settings):
# image handling
path_to_assets = "webui/flet/assets"
path_to_uploads = "webui/flet/uploads"
def create_blank_image():
img = Image.new('RGBA',(512,512),(0,0,0,0))
return img
def get_image_from_uploads(name):
path_to_image = os.path.join(path_to_uploads, name)
if os.path.exists(path_to_image):
img = Image.open(path_to_image)
return {name:img}
else:
log_message(f'image not found: "{name}"')
return {name:None}
# textual inversion

View File

@ -171,8 +171,31 @@ def main(page: ft.Page):
file_list.append(img)
file_picker.upload(file_list)
def upload_complete(e):
progress_bars.clear()
selected_files.controls.clear()
close_upload_window(e)
page.message('File upload(s) complete.')
layer_manager.add_images_as_layers(file_picker.images)
file_picker.images.clear()
selected_files = ft.Column();
def get_image_from_uploads(name):
return flet_utils.get_image_from_uploads(name)
def get_file_display(name, progress):
display = ft.Column(
controls = [
ft.Row([ft.Text(name)]),
progress,
],
)
return display
selected_files = ft.Column(
scroll = 'auto',
tight = True,
controls = [],
);
progress_bars: Dict[str, ft.ProgressBar] = {}
upload_window = ft.AlertDialog(
@ -225,7 +248,8 @@ def main(page: ft.Page):
color = 'blue',
)
progress_bars[f.name] = prog
selected_files.controls.append(ft.Row([ft.Text(f.name),prog]))
selected_files.controls.append(get_file_display(f.name,prog))
file_picker.pending += 1
# import if local, upload if remote
if not e.page.web:
open_import_window(e)
@ -234,18 +258,25 @@ def main(page: ft.Page):
def on_image_upload(e: ft.FilePickerUploadEvent):
if e.error:
page.message("Upload error occurred! Try again.",1)
return
# update progress bars
progress_bars[e.file_name].value = e.progress
progress_bars[e.file_name].update()
page.message(f"Upload error occurred! Failed to fetch '{e.file_name}'.",1)
file_picker.pending -= 1
else:
# update progress bars
progress_bars[e.file_name].value = e.progress
progress_bars[e.file_name].update()
if e.progress >= 1:
file_picker.pending -= 1
file_picker.images.update(get_image_from_uploads(e.file_name))
if file_picker.pending <= 0:
file_picker.pending = 0
upload_complete(e)
file_picker = ft.FilePicker(
on_result = pick_images,
on_upload = on_image_upload
)
file_picker.pending = 0
file_picker.images = {}
page.overlay.append(file_picker)
# layouts ############################################################