fix: name of method and formatting

This commit is contained in:
Artemy 2023-04-16 18:40:08 +03:00
parent 1b259c886f
commit fbc9112fba
9 changed files with 61 additions and 45 deletions

View file

@ -1,7 +1,7 @@
from PIL import Image, ImageFilter from PIL import Image, ImageFilter
def canny_edge(file_name): def edge(file_name):
image = Image.open(file_name) image = Image.open(file_name)
image = image.convert("L") image = image.convert("L")

View file

@ -6,7 +6,6 @@ def denoise(files):
bias = 1 bias = 1
image = Image.open(files[0]) image = Image.open(files[0])
for file in tqdm(files): for file in tqdm(files):
alpha = 1 / bias alpha = 1 / bias
im2 = Image.open(file) im2 = Image.open(file)
@ -31,7 +30,7 @@ def startracks(files):
def noise_extractor(files): def noise_extractor(files):
image = Image.open(files[0]) image = Image.open(files[0])
for file in tqdm(files, unit=' images'): for file in tqdm(files, unit=" images"):
im2 = Image.open(file) im2 = Image.open(file)
im3 = ImageChops.difference(image, im2) im3 = ImageChops.difference(image, im2)
image = im3 image = im3
@ -41,7 +40,7 @@ def noise_extractor(files):
def untrack(files): def untrack(files):
image = Image.open(files[0]) image = Image.open(files[0])
for file in tqdm(files, unit=' images'): for file in tqdm(files, unit=" images"):
im2 = Image.open(file) im2 = Image.open(file)
im3 = ImageChops.darker(image, im2) im3 = ImageChops.darker(image, im2)
image = im3 image = im3

View file

@ -1,35 +1,38 @@
import ffmpeg import ffmpeg
import os import os
from processing.utils import generate_name, get_date_text, generate_name_with_file_name from processing.utils import generate_name, get_date_text, generate_name_with_file_name
from methods.bulk_methods import canny_edge, sharpen from methods.bulk_methods import edge, sharpen
def images_to_video(directory, fps, img_ext, img_name_format, video_name, video_ext, video_dir): def images_to_video(
directory, fps, img_ext, img_name_format, video_name, video_ext, video_dir
):
images_pattern = os.path.join(directory, f"{img_name_format}.{img_ext}") images_pattern = os.path.join(directory, f"{img_name_format}.{img_ext}")
if video_dir: if video_dir:
video_path = os.path.join(video_dir, f"{video_name}.{video_ext}") video_path = os.path.join(video_dir, f"{video_name}.{video_ext}")
else: else:
video_path = generate_name( video_path = generate_name(
extension=video_ext, name=video_name, subfolder="videos") extension=video_ext, name=video_name, subfolder="videos"
)
ffmpeg.input(images_pattern, ffmpeg.input(images_pattern, framerate=fps).output(
framerate=fps).output(video_path, pix_fmt='yuv420p').global_args("-y").run() video_path, pix_fmt="yuv420p"
).global_args("-y").run()
def video_to_images(video_path, img_ext): def video_to_images(video_path, img_ext):
images_pattern = generate_name( images_pattern = generate_name(
extension=img_ext, name=f"%d", subfolder=os.path.join("images", get_date_text())) extension=img_ext, name=f"%d", subfolder=os.path.join("images", get_date_text())
)
ffmpeg.input(video_path).output(images_pattern).run() ffmpeg.input(video_path).output(images_pattern).run()
def bulk_processing(directory, out_directory, method): def bulk_processing(directory, out_directory, method):
date = get_date_text() date = get_date_text()
if method == "canny edge": if method == "Edge detection":
run_bulk(canny_edge, directory, out_directory, date) run_bulk(edge, directory, out_directory, date)
elif method == "sharpen": elif method == "sharpen":
run_bulk(sharpen, directory, out_directory, date) run_bulk(sharpen, directory, out_directory, date)
@ -41,5 +44,6 @@ def run_bulk(func, directory, out_directory, date):
img_out_path = os.path.join(out_directory, file) img_out_path = os.path.join(out_directory, file)
else: else:
img_out_path = generate_name_with_file_name( img_out_path = generate_name_with_file_name(
name=file, subfolder=os.path.join("images", date)) name=file, subfolder=os.path.join("images", date)
)
img.save(img_out_path) img.save(img_out_path)

View file

@ -4,7 +4,6 @@ from processing.utils import generate_name
def stacking(dir, method): def stacking(dir, method):
files = os.listdir(dir) files = os.listdir(dir)
files = list(map(lambda x: os.path.join(dir, x), files)) files = list(map(lambda x: os.path.join(dir, x), files))
files = list(filter(lambda x: x.endswith(".png"), files)) files = list(filter(lambda x: x.endswith(".png"), files))

View file

@ -2,8 +2,9 @@ import os
import datetime import datetime
def generate_name(name=False, subfolder="stacked", extension="png", format='%Y-%m-%d_%H-%M-%S'): def generate_name(
name=False, subfolder="stacked", extension="png", format="%Y-%m-%d_%H-%M-%S"
):
os.makedirs(os.path.join(".", "output", subfolder), exist_ok=True) os.makedirs(os.path.join(".", "output", subfolder), exist_ok=True)
if name is False or name == "": if name is False or name == "":
@ -13,11 +14,10 @@ def generate_name(name=False, subfolder="stacked", extension="png", format='%Y-%
def generate_name_with_file_name(name, subfolder): def generate_name_with_file_name(name, subfolder):
os.makedirs(os.path.join(".", "output", subfolder), exist_ok=True) os.makedirs(os.path.join(".", "output", subfolder), exist_ok=True)
return os.path.join(".", "output", subfolder, name) return os.path.join(".", "output", subfolder, name)
def get_date_text(format='%Y-%m-%d_%H-%M-%S'): def get_date_text(format="%Y-%m-%d_%H-%M-%S"):
return datetime.datetime.now().strftime(format) return datetime.datetime.now().strftime(format)

View file

@ -3,17 +3,22 @@ from processing.bulk import bulk_processing
with gr.Blocks() as app: with gr.Blocks() as app:
gr.Markdown( gr.Markdown(
"Mass processing of images one at a time and saving to video if needed. # **WIP, not working**") "Mass processing of images one at a time and saving to video if needed. # **WIP, not working**"
)
with gr.Row(): with gr.Row():
with gr.Column(): with gr.Column():
directory = gr.Text( directory = gr.Text(
placeholder="A directory with many images.", lines=1, label="Directory") placeholder="A directory with many images.", lines=1, label="Directory"
)
method = gr.Dropdown( method = gr.Dropdown(
choices=["canny edge", "sharpen"], value="canny edge", label="Method") choices=["canny edge", "sharpen"], value="canny edge", label="Method"
)
with gr.Accordion("Advanced settings", open=False) as acc: with gr.Accordion("Advanced settings", open=False) as acc:
out_dir = gr.Text( out_dir = gr.Text(
label="Output directory", placeholder="The directory where the processed photos will be saved. If not specified `./output/images`") label="Output directory",
placeholder="The directory where the processed photos will be saved. If not specified `./output/images`",
)
submit = gr.Button("Submit") submit = gr.Button("Submit")
submit.click( submit.click(
fn=bulk_processing, fn=bulk_processing,

View file

@ -4,27 +4,42 @@ from processing.bulk import images_to_video
with gr.Blocks() as app: with gr.Blocks() as app:
gr.Markdown("Convert images to video.") gr.Markdown("Convert images to video.")
directory = gr.Text( directory = gr.Text(
placeholder="A directory with many images of the same size", label="Directory") placeholder="A directory with many images of the same size", label="Directory"
)
fps = gr.Number(label="FPS", value=30, min=0) fps = gr.Number(label="FPS", value=30, min=0)
with gr.Accordion("Advanced settings", open=False) as acc: with gr.Accordion("Advanced settings", open=False) as acc:
video_name = gr.Text( video_name = gr.Text(
label="Video name", placeholder="Video name e.g. video. If not specified will be generated by time") label="Video name",
video_ext = gr.Dropdown(label="Video extension", choices=[ placeholder="Video name e.g. video. If not specified will be generated by time",
"mp4"], value="mp4") )
video_ext = gr.Dropdown(label="Video extension", choices=["mp4"], value="mp4")
video_dir = gr.Text( video_dir = gr.Text(
label="Video directory", placeholder="The directory where the video will be saved. If not specified `./output/videos`") label="Video directory",
placeholder="The directory where the video will be saved. If not specified `./output/videos`",
)
img_ext = gr.Dropdown(label="Image extension", choices=[ img_ext = gr.Dropdown(
"png", "jpg"], value="png") label="Image extension", choices=["png", "jpg"], value="png"
)
img_name_format = gr.Text( img_name_format = gr.Text(
label="Image name format", placeholder="ffmpeg pattern e.g. %04d is (0000.png)", value="%d") label="Image name format",
placeholder="ffmpeg pattern e.g. %04d is (0000.png)",
value="%d",
)
submit = gr.Button("Submit") submit = gr.Button("Submit")
submit.click( submit.click(
fn=images_to_video, fn=images_to_video,
inputs=[directory, fps, img_ext, img_name_format, inputs=[
video_name, video_ext, video_dir], directory,
fps,
img_ext,
img_name_format,
video_name,
video_ext,
video_dir,
],
) )

View file

@ -2,15 +2,14 @@ import gradio as gr
from processing.bulk import video_to_images from processing.bulk import video_to_images
with gr.Blocks() as app: with gr.Blocks() as app:
gr.Markdown( gr.Markdown("Convert video to images.")
"Convert video to images.")
with gr.Row(): with gr.Row():
with gr.Column(): with gr.Column():
video = gr.Video(label="Video") video = gr.Video(label="Video")
with gr.Column(): with gr.Column():
format = gr.Radio( format = gr.Radio(
choices=["png", "jpg"], value="png", label="Format of image") choices=["png", "jpg"], value="png", label="Format of image"
)
submit = gr.Button("Submit") submit = gr.Button("Submit")
submit.click(fn=video_to_images, inputs=[video, format]) submit.click(fn=video_to_images, inputs=[video, format])

View file

@ -1,6 +1 @@
tabs = [ tabs = ["Stacking", "Bulk processing", "Images to video", "Video to images"]
"Stacking",
"Bulk processing",
"Images to video",
"Video to images"
]