From efcd1ccbacd45b1ed3002cd46789a2d1eb9a5713 Mon Sep 17 00:00:00 2001 From: Artemy Date: Sat, 15 Apr 2023 19:36:31 +0300 Subject: [PATCH] feat: some not working tabs --- .gitignore | 2 + app.py | 137 ++++++++++++++------------------------- methods/stack_methods.py | 49 ++++++++++++++ processing/stacking.py | 30 +++++++++ 4 files changed, 128 insertions(+), 90 deletions(-) create mode 100644 methods/stack_methods.py create mode 100644 processing/stacking.py diff --git a/.gitignore b/.gitignore index d9005f2..59a443a 100644 --- a/.gitignore +++ b/.gitignore @@ -150,3 +150,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +output \ No newline at end of file diff --git a/app.py b/app.py index fbf0e24..a0a8d2c 100644 --- a/app.py +++ b/app.py @@ -1,98 +1,55 @@ -import datetime import gradio as gr -from PIL import Image, ImageChops -import os -from tqdm import tqdm - - -def impluser(dir, method): - - files = os.listdir(dir) - files = list(map(lambda x: os.path.join(dir, x), files)) - files = list(filter(lambda x: x.endswith(".png"), files)) - - if method == "denoise": - img = denoise(files) - elif method == "startracks": - img = startracks(files) - elif method == "noise extractor": - img = noise_extractor(files) - elif method == "untrack": - img = untrack(files) - - name = generate_name() - img.save(name) - - return [name] - - -def generate_name(): - # if not exists output create it - if not os.path.exists("./output"): - os.mkdir("./output") - - return f"./output/{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.png" - - -def denoise(files): - bias = 1 - image = Image.open(files[0]) - for file in tqdm(files): - - alpha = 1/bias - - im2 = Image.open(file) - im3 = Image.blend(image, im2, alpha) - - image = im3 - - bias += 1 - - return image - - -def startracks(files): - image = Image.open(files[0]) - for file in tqdm(files): - im2 = Image.open(file) - im3 = ImageChops.lighter(image, im2) - image = im3 - - return image - - -def noise_extractor(files): - image = Image.open(files[0]) - for file in tqdm(files, unit=' images'): - im2 = Image.open(file) - im3 = ImageChops.difference(image, im2) - image = im3 - - return image - - -def untrack(files): - image = Image.open(files[0]) - for file in tqdm(files, unit=' images'): - im2 = Image.open(file) - im3 = ImageChops.darker(image, im2) - image = im3 - - return image +from processing.stacking import stacking with gr.Blocks() as app: - with gr.Row(): - with gr.Column(): - directory = gr.Textbox( - placeholder="A directory on the same machine where the server is running.", lines=1, label="Directory") - methods = gr.Dropdown( - choices=["denoise", "startracks", "noise extractor", "untrack"], value="denoise", label="Method") - submit = gr.Button("Submit") + with gr.Tab("Stacking"): + gr.Markdown("Stacking images.") + with gr.Row(): + with gr.Column(): + directory = gr.Text( + placeholder="A directory with many images of the same size", lines=1, label="Directory") + methods = gr.Dropdown( + choices=["denoise", "startracks", "noise extractor", "untrack"], value="denoise", label="Method") + submit = gr.Button("Submit") - with gr.Column(): - output = gr.Gallery() + with gr.Column(): + output = gr.Gallery() + + submit.click(stacking, inputs=[ + directory, methods], outputs=[output]) + + with gr.Tab("Bulk processing"): + gr.Markdown( + "Mass processing of images one at a time and saving to video if needed. # **WIP, not working**") + with gr.Row(): + with gr.Column(): + directory = gr.Text( + placeholder="A directory with many images of the same size", lines=1, label="Directory") + methods = gr.Dropdown( + choices=["denoise", "startracks", "noise extractor", "untrack"], value="denoise", label="Method") + submit = gr.Button("Submit") + + with gr.Tab("Video to images"): + gr.Markdown( + "Convert video to images. # **WIP, not working**") + with gr.Row(): + with gr.Column(): + video = gr.Video(label="Video") + + format = gr.Radio( + choices=["png", "jpg"], value="png", label="Format of image") + submit = gr.Button("Submit") + + with gr.Tab("Images to video"): + gr.Markdown("Convert images to video. # **WIP, not working**") + with gr.Row(): + with gr.Column(): + directory = gr.Text( + placeholder="A directory with many images of the same size", lines=1, label="Directory") + + fps = gr.Number(label="FPS") + submit = gr.Button("Submit") - submit.click(impluser, inputs=[directory, methods], outputs=[output]) app.launch() diff --git a/methods/stack_methods.py b/methods/stack_methods.py new file mode 100644 index 0000000..5efff50 --- /dev/null +++ b/methods/stack_methods.py @@ -0,0 +1,49 @@ +from PIL import Image, ImageChops +from tqdm import tqdm + + +def denoise(files): + bias = 1 + image = Image.open(files[0]) + for file in tqdm(files): + + alpha = 1/bias + + im2 = Image.open(file) + im3 = Image.blend(image, im2, alpha) + + image = im3 + + bias += 1 + + return image + + +def startracks(files): + image = Image.open(files[0]) + for file in tqdm(files): + im2 = Image.open(file) + im3 = ImageChops.lighter(image, im2) + image = im3 + + return image + + +def noise_extractor(files): + image = Image.open(files[0]) + for file in tqdm(files, unit=' images'): + im2 = Image.open(file) + im3 = ImageChops.difference(image, im2) + image = im3 + + return image + + +def untrack(files): + image = Image.open(files[0]) + for file in tqdm(files, unit=' images'): + im2 = Image.open(file) + im3 = ImageChops.darker(image, im2) + image = im3 + + return image diff --git a/processing/stacking.py b/processing/stacking.py new file mode 100644 index 0000000..8e38797 --- /dev/null +++ b/processing/stacking.py @@ -0,0 +1,30 @@ +from methods.stack_methods import denoise, startracks, noise_extractor, untrack +import os +import datetime + + +def generate_name(): + os.makedirs("./output/stacked", exist_ok=True) + + return f"./output/stacked/{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.png" + + +def stacking(dir, method): + + files = os.listdir(dir) + files = list(map(lambda x: os.path.join(dir, x), files)) + files = list(filter(lambda x: x.endswith(".png"), files)) + + if method == "denoise": + img = denoise(files) + elif method == "startracks": + img = startracks(files) + elif method == "noise extractor": + img = noise_extractor(files) + elif method == "untrack": + img = untrack(files) + + name = generate_name() + img.save(name) + + return [name]