Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .gitignore +2 -0
- app.py +81 -0
- requirements.txt +1 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flagged
|
| 2 |
+
caches
|
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def convert_to_webp(input_image: str = None, quality=80):
|
| 7 |
+
file_path = Path("caches") / "{}.{}".format(Path(input_image).stem, "webp")
|
| 8 |
+
file_path.parent.mkdir(parents=True, exist_ok=True)
|
| 9 |
+
img = Image.open(input_image)
|
| 10 |
+
img = img.convert("RGBA")
|
| 11 |
+
img.save(file_path, "WEBP", quality=quality)
|
| 12 |
+
|
| 13 |
+
# reopen and check
|
| 14 |
+
img_reopen = Image.open(file_path)
|
| 15 |
+
img_reopen = img_reopen.convert("RGBA")
|
| 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 = convert_to_webp(path[0], quality)
|
| 24 |
+
out_files.append(file_path)
|
| 25 |
+
out_images.append(img_reopen)
|
| 26 |
+
return out_files, out_images
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def run(server_name="127.0.0.1", server_port=7860):
|
| 30 |
+
with gr.Blocks() as app:
|
| 31 |
+
gr.Markdown(
|
| 32 |
+
"""
|
| 33 |
+
# WebP Converter
|
| 34 |
+
Upload one or more image files and convert it to WebP format with adjustable quality.
|
| 35 |
+

|
| 36 |
+
"""
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
with gr.Row(equal_height=False):
|
| 40 |
+
with gr.Column():
|
| 41 |
+
inputs = [
|
| 42 |
+
gr.Gallery(
|
| 43 |
+
label="Input images",
|
| 44 |
+
show_label=True,
|
| 45 |
+
elem_id="gallery",
|
| 46 |
+
object_fit="contain",
|
| 47 |
+
height="auto",
|
| 48 |
+
columns=4,
|
| 49 |
+
# min_width=100,
|
| 50 |
+
),
|
| 51 |
+
gr.Slider(
|
| 52 |
+
minimum=1,
|
| 53 |
+
maximum=100,
|
| 54 |
+
value=80,
|
| 55 |
+
step=1,
|
| 56 |
+
label="Quality",
|
| 57 |
+
),
|
| 58 |
+
]
|
| 59 |
+
btn = gr.Button("Converted WebP", variant="primary")
|
| 60 |
+
|
| 61 |
+
with gr.Column():
|
| 62 |
+
outputs = [
|
| 63 |
+
gr.File(label="Converted WebP"),
|
| 64 |
+
gr.Gallery(
|
| 65 |
+
label="Re-check converted images",
|
| 66 |
+
show_label=False,
|
| 67 |
+
elem_id="gallery",
|
| 68 |
+
object_fit="contain",
|
| 69 |
+
height="auto",
|
| 70 |
+
columns=4,
|
| 71 |
+
),
|
| 72 |
+
]
|
| 73 |
+
|
| 74 |
+
btn.click(process, inputs=inputs, outputs=outputs)
|
| 75 |
+
app.queue().launch(
|
| 76 |
+
server_name=server_name, server_port=server_port, share=False
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
run(server_name="0.0.0.0", server_port=7862)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio==4.25.0
|