Spaces:
Running
Running
Vincentqyw
commited on
Commit
·
63d332a
1
Parent(s):
32fba57
update: app
Browse files
app.py
CHANGED
|
@@ -3,12 +3,27 @@ from pathlib import Path
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
file_path.parent.mkdir(parents=True, exist_ok=True)
|
| 9 |
img = Image.open(input_image)
|
| 10 |
-
img = img.convert("RGBA")
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# reopen and check
|
| 14 |
img_reopen = Image.open(file_path)
|
|
@@ -16,11 +31,11 @@ def convert_to_webp(input_image: str = None, quality=80):
|
|
| 16 |
return img_reopen, str(file_path)
|
| 17 |
|
| 18 |
|
| 19 |
-
def process(input_list, quality=80):
|
| 20 |
out_files = []
|
| 21 |
out_images = []
|
| 22 |
for path in input_list:
|
| 23 |
-
img_reopen, file_path =
|
| 24 |
out_files.append(file_path)
|
| 25 |
out_images.append(img_reopen)
|
| 26 |
return out_files, out_images
|
|
@@ -34,6 +49,12 @@ def swap_to_gallery(images):
|
|
| 34 |
)
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def run(server_name="127.0.0.1", server_port=7860):
|
| 38 |
with gr.Blocks() as app:
|
| 39 |
gr.Markdown(
|
|
@@ -53,38 +74,59 @@ def run(server_name="127.0.0.1", server_port=7860):
|
|
| 53 |
uploaded_files = gr.Gallery(
|
| 54 |
label="Your images", visible=False, columns=4, height=250
|
| 55 |
)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
gr.Slider(
|
| 59 |
minimum=1,
|
| 60 |
maximum=100,
|
| 61 |
value=80,
|
| 62 |
step=1,
|
| 63 |
label="Image Quality",
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
with gr.Column():
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
files.upload(
|
| 82 |
fn=swap_to_gallery,
|
| 83 |
inputs=files,
|
| 84 |
-
outputs=[uploaded_files,
|
| 85 |
)
|
| 86 |
-
|
| 87 |
-
#
|
| 88 |
app.queue().launch(
|
| 89 |
server_name=server_name, server_port=server_port, share=False
|
| 90 |
)
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
|
| 6 |
+
def get_supported_formats():
|
| 7 |
+
supported_formats = Image.registered_extensions()
|
| 8 |
+
return supported_formats
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
SUPPORTED_FORMATS = get_supported_formats()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def convert_format(input_image: str = None, ext=".webp", quality=80):
|
| 15 |
+
file_path = Path("caches") / "{}{}".format(Path(input_image).stem, ext)
|
| 16 |
file_path.parent.mkdir(parents=True, exist_ok=True)
|
| 17 |
img = Image.open(input_image)
|
| 18 |
+
# img = img.convert("RGBA")
|
| 19 |
+
format = None
|
| 20 |
+
if ext in SUPPORTED_FORMATS:
|
| 21 |
+
format = SUPPORTED_FORMATS[ext]
|
| 22 |
+
if format is None:
|
| 23 |
+
gr.Error(
|
| 24 |
+
f"Unsupported image format. Supported formats: {', '.join(SUPPORTED_FORMATS)}"
|
| 25 |
+
)
|
| 26 |
+
img.save(file_path, format, quality=quality)
|
| 27 |
|
| 28 |
# reopen and check
|
| 29 |
img_reopen = Image.open(file_path)
|
|
|
|
| 31 |
return img_reopen, str(file_path)
|
| 32 |
|
| 33 |
|
| 34 |
+
def process(input_list, ext=".webp", quality=80):
|
| 35 |
out_files = []
|
| 36 |
out_images = []
|
| 37 |
for path in input_list:
|
| 38 |
+
img_reopen, file_path = convert_format(path[0], ext, quality)
|
| 39 |
out_files.append(file_path)
|
| 40 |
out_images.append(img_reopen)
|
| 41 |
return out_files, out_images
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
|
| 52 |
+
def download_files(files):
|
| 53 |
+
for file in files:
|
| 54 |
+
breakpoint()
|
| 55 |
+
gr.DownloadButton(visible=True, value=file)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
def run(server_name="127.0.0.1", server_port=7860):
|
| 59 |
with gr.Blocks() as app:
|
| 60 |
gr.Markdown(
|
|
|
|
| 74 |
uploaded_files = gr.Gallery(
|
| 75 |
label="Your images", visible=False, columns=4, height=250
|
| 76 |
)
|
| 77 |
+
with gr.Row():
|
| 78 |
+
quality_slider = gr.Slider(
|
|
|
|
| 79 |
minimum=1,
|
| 80 |
maximum=100,
|
| 81 |
value=80,
|
| 82 |
step=1,
|
| 83 |
label="Image Quality",
|
| 84 |
+
)
|
| 85 |
+
extension_dropdown = gr.Dropdown(
|
| 86 |
+
label="Output Format",
|
| 87 |
+
choices=[
|
| 88 |
+
".webp",
|
| 89 |
+
".png",
|
| 90 |
+
".jpg",
|
| 91 |
+
".jpeg",
|
| 92 |
+
".gif",
|
| 93 |
+
".bmp",
|
| 94 |
+
".tiff",
|
| 95 |
+
".tif",
|
| 96 |
+
],
|
| 97 |
+
value=".webp",
|
| 98 |
+
)
|
| 99 |
+
proc_btn = gr.Button("Run Convert", variant="primary")
|
| 100 |
|
| 101 |
with gr.Column():
|
| 102 |
+
output_file = gr.File(label="Converted WebP")
|
| 103 |
+
output_gallery = gr.Gallery(
|
| 104 |
+
label="Re-check converted images",
|
| 105 |
+
show_label=False,
|
| 106 |
+
elem_id="gallery",
|
| 107 |
+
object_fit="contain",
|
| 108 |
+
height="auto",
|
| 109 |
+
columns=4,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# collect inputs and outputs
|
| 113 |
+
inputs = [
|
| 114 |
+
uploaded_files,
|
| 115 |
+
extension_dropdown,
|
| 116 |
+
quality_slider,
|
| 117 |
+
]
|
| 118 |
+
outputs = [
|
| 119 |
+
output_file,
|
| 120 |
+
output_gallery,
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
# actions
|
| 124 |
files.upload(
|
| 125 |
fn=swap_to_gallery,
|
| 126 |
inputs=files,
|
| 127 |
+
outputs=[uploaded_files, proc_btn, files],
|
| 128 |
)
|
| 129 |
+
proc_btn.click(process, inputs=inputs, outputs=outputs)
|
|
|
|
| 130 |
app.queue().launch(
|
| 131 |
server_name=server_name, server_port=server_port, share=False
|
| 132 |
)
|