Spaces:
Running
Running
Vincentqyw
commited on
Commit
·
428baa9
1
Parent(s):
63d332a
update: app
Browse files
app.py
CHANGED
|
@@ -4,6 +4,11 @@ from PIL import Image
|
|
| 4 |
|
| 5 |
|
| 6 |
def get_supported_formats():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
supported_formats = Image.registered_extensions()
|
| 8 |
return supported_formats
|
| 9 |
|
|
@@ -11,7 +16,20 @@ def get_supported_formats():
|
|
| 11 |
SUPPORTED_FORMATS = get_supported_formats()
|
| 12 |
|
| 13 |
|
| 14 |
-
def convert_format(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -21,7 +39,9 @@ def convert_format(input_image: str = None, ext=".webp", quality=80):
|
|
| 21 |
format = SUPPORTED_FORMATS[ext]
|
| 22 |
if format is None:
|
| 23 |
gr.Error(
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
img.save(file_path, format, quality=quality)
|
| 27 |
|
|
@@ -31,7 +51,18 @@ def convert_format(input_image: str = None, ext=".webp", quality=80):
|
|
| 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:
|
|
@@ -41,7 +72,10 @@ def process(input_list, ext=".webp", quality=80):
|
|
| 41 |
return out_files, out_images
|
| 42 |
|
| 43 |
|
| 44 |
-
def swap_to_gallery(images):
|
|
|
|
|
|
|
|
|
|
| 45 |
return (
|
| 46 |
gr.update(value=images, visible=True),
|
| 47 |
gr.update(visible=True),
|
|
@@ -49,13 +83,17 @@ def swap_to_gallery(images):
|
|
| 49 |
)
|
| 50 |
|
| 51 |
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
gr.DownloadButton(visible=True, value=file)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
with gr.Blocks() as app:
|
| 60 |
gr.Markdown(
|
| 61 |
"""
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def get_supported_formats():
|
| 7 |
+
"""
|
| 8 |
+
A function that retrieves the supported formats of images.
|
| 9 |
+
Returns:
|
| 10 |
+
The supported image formats as a list.
|
| 11 |
+
"""
|
| 12 |
supported_formats = Image.registered_extensions()
|
| 13 |
return supported_formats
|
| 14 |
|
|
|
|
| 16 |
SUPPORTED_FORMATS = get_supported_formats()
|
| 17 |
|
| 18 |
|
| 19 |
+
def convert_format(
|
| 20 |
+
input_image: str = None, ext: str = ".webp", quality: int = 80
|
| 21 |
+
):
|
| 22 |
+
"""
|
| 23 |
+
A function that converts an input image to a specified format with a given quality.
|
| 24 |
+
|
| 25 |
+
Parameters:
|
| 26 |
+
input_image (str): The path to the input image file.
|
| 27 |
+
ext (str, optional): The extension for the output format. Defaults to ".webp".
|
| 28 |
+
quality (int, optional): The quality of the output image. Defaults to 80.
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
tuple: A tuple containing the reopened image in RGBA format and the path to the saved image file.
|
| 32 |
+
"""
|
| 33 |
file_path = Path("caches") / "{}{}".format(Path(input_image).stem, ext)
|
| 34 |
file_path.parent.mkdir(parents=True, exist_ok=True)
|
| 35 |
img = Image.open(input_image)
|
|
|
|
| 39 |
format = SUPPORTED_FORMATS[ext]
|
| 40 |
if format is None:
|
| 41 |
gr.Error(
|
| 42 |
+
"Unsupported image format. Supported formats: {}".format(
|
| 43 |
+
", ".join(SUPPORTED_FORMATS)
|
| 44 |
+
)
|
| 45 |
)
|
| 46 |
img.save(file_path, format, quality=quality)
|
| 47 |
|
|
|
|
| 51 |
return img_reopen, str(file_path)
|
| 52 |
|
| 53 |
|
| 54 |
+
def process(input_list: list[tuple], ext: str = ".webp", quality: int = 80):
|
| 55 |
+
"""
|
| 56 |
+
A function that processes a list of images by converting them to a specified format with a given quality.
|
| 57 |
+
|
| 58 |
+
Parameters:
|
| 59 |
+
input_list (list[tuple]): A list of tuples containing the paths to the input image files.
|
| 60 |
+
ext (str, optional): The extension for the output format. Defaults to ".webp".
|
| 61 |
+
quality (int, optional): The quality of the output images. Defaults to 80.
|
| 62 |
+
|
| 63 |
+
Returns:
|
| 64 |
+
tuple: A tuple containing lists of file paths and reopened images in RGBA format.
|
| 65 |
+
"""
|
| 66 |
out_files = []
|
| 67 |
out_images = []
|
| 68 |
for path in input_list:
|
|
|
|
| 72 |
return out_files, out_images
|
| 73 |
|
| 74 |
|
| 75 |
+
def swap_to_gallery(images: list):
|
| 76 |
+
"""
|
| 77 |
+
A function that swaps to a gallery, taking a list of images as input.
|
| 78 |
+
"""
|
| 79 |
return (
|
| 80 |
gr.update(value=images, visible=True),
|
| 81 |
gr.update(visible=True),
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
|
| 86 |
+
def run(server_name: str = "127.0.0.1", server_port: int = 7860):
|
| 87 |
+
"""
|
| 88 |
+
A function that runs the WebP Converter app, allowing users to upload images, set quality, and convert them to WebP format.
|
|
|
|
| 89 |
|
| 90 |
+
Parameters:
|
| 91 |
+
server_name (str, optional): The server name where the app is hosted. Defaults to "127.0.0.1".
|
| 92 |
+
server_port (int, optional): The port number for the server. Defaults to 7860.
|
| 93 |
|
| 94 |
+
Returns:
|
| 95 |
+
None
|
| 96 |
+
"""
|
| 97 |
with gr.Blocks() as app:
|
| 98 |
gr.Markdown(
|
| 99 |
"""
|