Spaces:
Running
Running
tricking out with error logs and comments
Browse files
app.py
CHANGED
@@ -8,7 +8,8 @@ def get_supported_formats():
|
|
8 |
Returns:
|
9 |
The supported image formats as a list.
|
10 |
"""
|
11 |
-
supported_formats = Image.registered_extensions()
|
|
|
12 |
return supported_formats
|
13 |
|
14 |
|
@@ -29,24 +30,31 @@ def convert_format(
|
|
29 |
Returns:
|
30 |
tuple: A tuple containing the reopened image in RGBA format and the path to the saved image file.
|
31 |
"""
|
32 |
-
|
33 |
-
file_path.
|
34 |
-
|
35 |
-
#
|
|
|
|
|
|
|
36 |
format = None
|
37 |
-
if ext
|
|
|
38 |
format = SUPPORTED_FORMATS[ext]
|
39 |
-
if format is None:
|
40 |
-
|
41 |
-
"
|
42 |
-
", ".join(SUPPORTED_FORMATS)
|
43 |
-
)
|
44 |
)
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
#
|
48 |
img_reopen = Image.open(file_path)
|
49 |
-
img_reopen = img_reopen.convert("RGBA")
|
|
|
50 |
return img_reopen, str(file_path)
|
51 |
|
52 |
|
@@ -62,23 +70,32 @@ def process(input_list: list[tuple], ext: str = ".webp", quality: int = 80):
|
|
62 |
Returns:
|
63 |
tuple: A tuple containing lists of file paths and reopened images in RGBA format.
|
64 |
"""
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
71 |
return out_files, out_images
|
72 |
|
73 |
|
74 |
def swap_to_gallery(images: list):
|
75 |
"""
|
76 |
A function that swaps to a gallery, taking a list of images as input.
|
|
|
|
|
|
|
|
|
77 |
"""
|
|
|
78 |
return (
|
79 |
-
gr.update(value=images, visible=True),
|
80 |
-
gr.update(visible=True),
|
81 |
-
gr.update(visible=False),
|
82 |
)
|
83 |
|
84 |
|
@@ -93,7 +110,8 @@ def run(server_name: str = "127.0.0.1", server_port: int = 7860):
|
|
93 |
Returns:
|
94 |
None
|
95 |
"""
|
96 |
-
|
|
|
97 |
gr.Markdown(
|
98 |
"""
|
99 |
# Image Format Converter
|
@@ -102,20 +120,21 @@ def run(server_name: str = "127.0.0.1", server_port: int = 7860):
|
|
102 |
"""
|
103 |
)
|
104 |
|
105 |
-
with gr.Row(equal_height=False):
|
106 |
-
with gr.Column():
|
107 |
files = gr.Files(
|
108 |
-
label="Drag 1 or more images",
|
109 |
-
file_types=["image"],
|
110 |
)
|
111 |
uploaded_files = gr.Gallery(
|
112 |
label="Your images", visible=False, columns=4, height="auto"
|
113 |
-
)
|
114 |
-
|
|
|
115 |
quality_slider = gr.Slider(
|
116 |
minimum=1,
|
117 |
maximum=100,
|
118 |
-
value=80,
|
119 |
step=1,
|
120 |
label="Image Quality",
|
121 |
)
|
@@ -131,14 +150,14 @@ def run(server_name: str = "127.0.0.1", server_port: int = 7860):
|
|
131 |
".tiff",
|
132 |
".tif",
|
133 |
],
|
134 |
-
value=".webp",
|
135 |
)
|
136 |
-
with gr.Row():
|
137 |
-
reset_btn = gr.Button("Clear Images", variant="secondary")
|
138 |
-
proc_btn = gr.Button("Run Convert", variant="primary")
|
139 |
|
140 |
-
with gr.Column():
|
141 |
-
output_file = gr.File(label="Converted WebP")
|
142 |
output_gallery = gr.Gallery(
|
143 |
label="Re-check converted images",
|
144 |
show_label=False,
|
@@ -146,9 +165,9 @@ def run(server_name: str = "127.0.0.1", server_port: int = 7860):
|
|
146 |
object_fit="contain",
|
147 |
height="auto",
|
148 |
columns=4,
|
149 |
-
)
|
150 |
|
151 |
-
#
|
152 |
inputs = [
|
153 |
uploaded_files,
|
154 |
extension_dropdown,
|
@@ -159,15 +178,16 @@ def run(server_name: str = "127.0.0.1", server_port: int = 7860):
|
|
159 |
output_gallery,
|
160 |
]
|
161 |
|
162 |
-
# actions
|
163 |
files.upload(
|
164 |
-
fn=swap_to_gallery,
|
165 |
inputs=files,
|
166 |
outputs=[uploaded_files, proc_btn, files],
|
167 |
)
|
168 |
-
proc_btn.click(process, inputs=inputs, outputs=outputs)
|
169 |
-
reset_btn.click(lambda: None, None, uploaded_files, queue=False)
|
170 |
-
|
|
|
171 |
server_name=server_name, server_port=server_port, share=False
|
172 |
)
|
173 |
|
|
|
8 |
Returns:
|
9 |
The supported image formats as a list.
|
10 |
"""
|
11 |
+
supported_formats = Image.registered_extensions() # Get all registered image extensions and their formats
|
12 |
+
print("Supported formats retrieved:", supported_formats)
|
13 |
return supported_formats
|
14 |
|
15 |
|
|
|
30 |
Returns:
|
31 |
tuple: A tuple containing the reopened image in RGBA format and the path to the saved image file.
|
32 |
"""
|
33 |
+
print("Converting image:", input_image, "to format:", ext, "with quality:", quality)
|
34 |
+
file_path = Path("caches") / "{}{}".format(Path(input_image).stem, ext) # Generate output file path
|
35 |
+
print("Output file path generated:", file_path)
|
36 |
+
file_path.parent.mkdir(parents=True, exist_ok=True) # Create directories if they don't exist
|
37 |
+
img = Image.open(input_image) # Open the input image
|
38 |
+
print("Image opened successfully.")
|
39 |
+
|
40 |
format = None
|
41 |
+
print("Checking if extension is supported:", ext) # Log the extension being checked
|
42 |
+
if ext in SUPPORTED_FORMATS: # Check if the specified extension is supported
|
43 |
format = SUPPORTED_FORMATS[ext]
|
44 |
+
if format is None: # If format is not supported, raise an error
|
45 |
+
error_message = "Unsupported image format. Supported formats: {}".format(
|
46 |
+
", ".join(SUPPORTED_FORMATS)
|
|
|
|
|
47 |
)
|
48 |
+
print(error_message)
|
49 |
+
gr.Error(error_message)
|
50 |
+
|
51 |
+
img.save(file_path, format, quality=quality) # Save the image in the specified format and quality
|
52 |
+
print("Image saved to:", file_path)
|
53 |
|
54 |
+
# Reopen the saved image to verify its integrity
|
55 |
img_reopen = Image.open(file_path)
|
56 |
+
img_reopen = img_reopen.convert("RGBA") # Ensure the image is in RGBA format
|
57 |
+
print("Image reopened and converted to RGBA format.")
|
58 |
return img_reopen, str(file_path)
|
59 |
|
60 |
|
|
|
70 |
Returns:
|
71 |
tuple: A tuple containing lists of file paths and reopened images in RGBA format.
|
72 |
"""
|
73 |
+
print("Processing images with extension:", ext, "and quality:", quality)
|
74 |
+
out_files = [] # List to store paths of converted images
|
75 |
+
out_images = [] # List to store reopened images
|
76 |
+
for index, path in enumerate(input_list):
|
77 |
+
print(f"Processing image {index + 1}/{len(input_list)}: {path[0]}") # Include index for debugging batches
|
78 |
+
img_reopen, file_path = convert_format(path[0], ext, quality) # Convert each image
|
79 |
+
out_files.append(file_path) # Append the file path to the output list
|
80 |
+
out_images.append(img_reopen) # Append the reopened image to the output list
|
81 |
+
print("Image processed successfully.")
|
82 |
+
print("All images processed. Output files:", out_files)
|
83 |
return out_files, out_images
|
84 |
|
85 |
|
86 |
def swap_to_gallery(images: list):
|
87 |
"""
|
88 |
A function that swaps to a gallery, taking a list of images as input.
|
89 |
+
Parameters:
|
90 |
+
images (list): List of images to display in the gallery.
|
91 |
+
Returns:
|
92 |
+
Updated gallery state.
|
93 |
"""
|
94 |
+
print("Swapping to gallery with images:", images)
|
95 |
return (
|
96 |
+
gr.update(value=images, visible=True), # Update the gallery with new images
|
97 |
+
gr.update(visible=True), # Make the gallery visible
|
98 |
+
gr.update(visible=False), # Hide the file upload button
|
99 |
)
|
100 |
|
101 |
|
|
|
110 |
Returns:
|
111 |
None
|
112 |
"""
|
113 |
+
print("Starting the WebP Converter app on {}:{}".format(server_name, server_port))
|
114 |
+
with gr.Blocks(theme="Nymbo/Nymbo_Theme") as app: # Define the Gradio app layout
|
115 |
gr.Markdown(
|
116 |
"""
|
117 |
# Image Format Converter
|
|
|
120 |
"""
|
121 |
)
|
122 |
|
123 |
+
with gr.Row(equal_height=False): # Define a row layout with unequal column heights
|
124 |
+
with gr.Column(): # First column for user inputs
|
125 |
files = gr.Files(
|
126 |
+
label="Drag 1 or more images", # Upload button for images
|
127 |
+
file_types=["image"], # Restrict file types to images
|
128 |
)
|
129 |
uploaded_files = gr.Gallery(
|
130 |
label="Your images", visible=False, columns=4, height="auto"
|
131 |
+
) # Gallery to display uploaded images
|
132 |
+
|
133 |
+
with gr.Row(): # Row for quality slider and format dropdown
|
134 |
quality_slider = gr.Slider(
|
135 |
minimum=1,
|
136 |
maximum=100,
|
137 |
+
value=80, # Default quality
|
138 |
step=1,
|
139 |
label="Image Quality",
|
140 |
)
|
|
|
150 |
".tiff",
|
151 |
".tif",
|
152 |
],
|
153 |
+
value=".webp", # Default format
|
154 |
)
|
155 |
+
with gr.Row(): # Row for buttons
|
156 |
+
reset_btn = gr.Button("Clear Images", variant="secondary") # Clear button
|
157 |
+
proc_btn = gr.Button("Run Convert", variant="primary") # Convert button
|
158 |
|
159 |
+
with gr.Column(): # Second column for outputs
|
160 |
+
output_file = gr.File(label="Converted WebP") # Downloadable converted file
|
161 |
output_gallery = gr.Gallery(
|
162 |
label="Re-check converted images",
|
163 |
show_label=False,
|
|
|
165 |
object_fit="contain",
|
166 |
height="auto",
|
167 |
columns=4,
|
168 |
+
) # Gallery to display converted images
|
169 |
|
170 |
+
# Collect inputs and outputs for processing
|
171 |
inputs = [
|
172 |
uploaded_files,
|
173 |
extension_dropdown,
|
|
|
178 |
output_gallery,
|
179 |
]
|
180 |
|
181 |
+
# Define actions for user interactions
|
182 |
files.upload(
|
183 |
+
fn=swap_to_gallery, # Action when files are uploaded
|
184 |
inputs=files,
|
185 |
outputs=[uploaded_files, proc_btn, files],
|
186 |
)
|
187 |
+
proc_btn.click(process, inputs=inputs, outputs=outputs) # Action when convert button is clicked
|
188 |
+
reset_btn.click(lambda: None, None, uploaded_files, queue=False) # Action when clear button is clicked
|
189 |
+
|
190 |
+
app.queue().launch( # Launch the app
|
191 |
server_name=server_name, server_port=server_port, share=False
|
192 |
)
|
193 |
|