devskale
commited on
Commit
·
8f00e6a
1
Parent(s):
b8622fd
cleaned up
Browse files- .gitignore +1 -1
- data/.DS_Store +0 -0
- gradio_imager.py +144 -112
- gradio_test.py +47 -0
.gitignore
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
.venv
|
2 |
.gradio
|
3 |
__pycache__
|
4 |
-
**/.DS_Store
|
|
|
1 |
.venv
|
2 |
.gradio
|
3 |
__pycache__
|
4 |
+
**/.DS_Store
|
data/.DS_Store
DELETED
Binary file (6.15 kB)
|
|
gradio_imager.py
CHANGED
@@ -5,7 +5,6 @@ import os
|
|
5 |
from image_processor import process_image, save_image_with_format
|
6 |
import io
|
7 |
|
8 |
-
|
9 |
def apply_standard_settings(setting):
|
10 |
"""Returns the parameters for the selected standard setting."""
|
11 |
settings_dict = {
|
@@ -18,8 +17,6 @@ def apply_standard_settings(setting):
|
|
18 |
}
|
19 |
return settings_dict.get(setting, (None, None, None, None, None))
|
20 |
|
21 |
-
|
22 |
-
# Update settings description for simpler UI
|
23 |
def settings_description(crop, remove_bg, resize, padding, background, quality):
|
24 |
"""Generate an HTML text description of the current settings."""
|
25 |
description = f"""
|
@@ -35,72 +32,59 @@ def settings_description(crop, remove_bg, resize, padding, background, quality):
|
|
35 |
return description
|
36 |
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
import io
|
43 |
-
|
44 |
-
if image is None:
|
45 |
-
return ""
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
def get_formatted_filename(original_name, width, height, format):
|
76 |
-
"""Generate formatted filename with size."""
|
77 |
-
# Remove extension from original name
|
78 |
-
base_name = os.path.splitext(os.path.basename(original_name))[0]
|
79 |
-
return f"{base_name}_{width}x{height}.{format}"
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
def gradio_interface(image, standard_settings, crop=False, remove_bg=False,
|
84 |
resize=None, padding=0, background="white",
|
85 |
quality=90):
|
86 |
"""Main interface function for the Gradio app."""
|
87 |
quality = int(quality)
|
88 |
|
89 |
-
if
|
90 |
standard_image_path = './data/examples/supermario.png'
|
91 |
-
|
92 |
-
|
93 |
-
# Get original filename without extension
|
94 |
-
if isinstance(image, dict) and 'name' in image: # Check if image contains metadata
|
95 |
-
original_name = os.path.splitext(image['name'])[0]
|
96 |
-
elif hasattr(image, 'filename') and image.filename:
|
97 |
-
original_name = os.path.splitext(os.path.basename(image.filename))[0]
|
98 |
else:
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
|
102 |
-
if isinstance(
|
103 |
-
print(f"Debug - Image dict: {image.keys()}") # Debug line
|
104 |
|
105 |
if standard_settings and standard_settings != "None":
|
106 |
crop, remove_bg, resize, padding, background = apply_standard_settings(standard_settings)
|
@@ -123,6 +107,12 @@ def gradio_interface(image, standard_settings, crop=False, remove_bg=False,
|
|
123 |
background=background
|
124 |
)
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
# Get temp directory for saving files
|
127 |
temp_dir = tempfile.gettempdir()
|
128 |
|
@@ -159,7 +149,8 @@ def gradio_interface(image, standard_settings, crop=False, remove_bg=False,
|
|
159 |
crop, remove_bg, resize, padding, background, quality
|
160 |
)
|
161 |
|
162 |
-
|
|
|
163 |
downloads['webp'],
|
164 |
downloads['png'],
|
165 |
downloads['jpg'],
|
@@ -178,54 +169,105 @@ example_images = [
|
|
178 |
]
|
179 |
|
180 |
|
181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
gr.Markdown("# 🖼️ IMAGER v0.2 - Image Processing Tool 🛠️")
|
183 |
gr.Markdown("📤 Upload an image and select one of the processing options below to get started! 🚀")
|
184 |
-
|
|
|
185 |
with gr.Column(scale=1):
|
186 |
-
#
|
187 |
-
|
188 |
-
|
|
|
|
|
189 |
|
190 |
-
#
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
with gr.Column(scale=1):
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
process_btn.click(
|
226 |
fn=gradio_interface,
|
227 |
inputs=[
|
228 |
-
|
229 |
padding, background, quality
|
230 |
],
|
231 |
outputs=[
|
@@ -233,15 +275,5 @@ with gr.Blocks(title="IMAGER ___ v0.2 Image Processor") as interface:
|
|
233 |
]
|
234 |
)
|
235 |
|
236 |
-
# Examples
|
237 |
-
gr.Examples(
|
238 |
-
examples=example_images,
|
239 |
-
inputs=[
|
240 |
-
input_image, settings, crop, remove_bg, resize,
|
241 |
-
padding, background, quality
|
242 |
-
]
|
243 |
-
)
|
244 |
-
|
245 |
-
|
246 |
if __name__ == "__main__":
|
247 |
-
|
|
|
5 |
from image_processor import process_image, save_image_with_format
|
6 |
import io
|
7 |
|
|
|
8 |
def apply_standard_settings(setting):
|
9 |
"""Returns the parameters for the selected standard setting."""
|
10 |
settings_dict = {
|
|
|
17 |
}
|
18 |
return settings_dict.get(setting, (None, None, None, None, None))
|
19 |
|
|
|
|
|
20 |
def settings_description(crop, remove_bg, resize, padding, background, quality):
|
21 |
"""Generate an HTML text description of the current settings."""
|
22 |
description = f"""
|
|
|
32 |
return description
|
33 |
|
34 |
|
35 |
+
def get_file_info(file):
|
36 |
+
"""Get file info from the uploaded image file."""
|
37 |
+
if file is None:
|
38 |
+
return "No file uploaded."
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
try:
|
41 |
+
# For type="image", file is a dict with keys 'path' and 'name'
|
42 |
+
if isinstance(file, dict):
|
43 |
+
original_name = os.path.splitext(os.path.basename(file["name"]))[0]
|
44 |
+
file_path = file["path"]
|
45 |
+
else:
|
46 |
+
original_name = os.path.splitext(os.path.basename(file.name))[0]
|
47 |
+
file_path = file.name
|
48 |
+
|
49 |
+
# Open the image file
|
50 |
+
image = Image.open(file_path)
|
51 |
+
|
52 |
+
# Get image dimensions
|
53 |
+
width, height = image.size
|
54 |
+
|
55 |
+
# Calculate file size
|
56 |
+
size_bytes = os.path.getsize(file_path)
|
57 |
+
size_kb = size_bytes / 1024
|
58 |
+
|
59 |
+
return f"""<div style="text-align: left; padding: 5px;">
|
60 |
+
<p>Original Name: {original_name}</p>
|
61 |
+
<p>Size: {width}x{height}px ({size_kb:.1f} KB)</p>
|
62 |
+
</div>"""
|
63 |
+
except Exception as e:
|
64 |
+
return f"Error getting file info: {str(e)}"
|
65 |
+
|
66 |
+
|
67 |
+
def gradio_interface(file, standard_settings, crop=False, remove_bg=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
resize=None, padding=0, background="white",
|
69 |
quality=90):
|
70 |
"""Main interface function for the Gradio app."""
|
71 |
quality = int(quality)
|
72 |
|
73 |
+
if file is None:
|
74 |
standard_image_path = './data/examples/supermario.png'
|
75 |
+
file = standard_image_path
|
76 |
+
original_name = os.path.splitext(os.path.basename(standard_image_path))[0]
|
|
|
|
|
|
|
|
|
|
|
77 |
else:
|
78 |
+
# Handle both dictionary (type="image") and file object cases
|
79 |
+
if isinstance(file, dict):
|
80 |
+
original_name = os.path.splitext(os.path.basename(file["name"]))[0]
|
81 |
+
file_path = file["path"]
|
82 |
+
else:
|
83 |
+
original_name = os.path.splitext(os.path.basename(file.name))[0]
|
84 |
+
file_path = file.name
|
85 |
|
86 |
+
# Open the image file
|
87 |
+
image = Image.open(file_path if isinstance(file, dict) else file)
|
|
|
88 |
|
89 |
if standard_settings and standard_settings != "None":
|
90 |
crop, remove_bg, resize, padding, background = apply_standard_settings(standard_settings)
|
|
|
107 |
background=background
|
108 |
)
|
109 |
|
110 |
+
# For preview, ensure we're working with RGB mode
|
111 |
+
preview_image = processed_image
|
112 |
+
if processed_image.mode in ('RGBA', 'LA'):
|
113 |
+
preview_image = Image.new('RGB', processed_image.size, 'white')
|
114 |
+
preview_image.paste(processed_image, mask=processed_image.split()[-1])
|
115 |
+
|
116 |
# Get temp directory for saving files
|
117 |
temp_dir = tempfile.gettempdir()
|
118 |
|
|
|
149 |
crop, remove_bg, resize, padding, background, quality
|
150 |
)
|
151 |
|
152 |
+
# Return the preview image and downloads
|
153 |
+
return (preview_image, # Return the actual image object for preview
|
154 |
downloads['webp'],
|
155 |
downloads['png'],
|
156 |
downloads['jpg'],
|
|
|
169 |
]
|
170 |
|
171 |
|
172 |
+
def preview_and_info(f):
|
173 |
+
"""Handle file preview and info, including None case"""
|
174 |
+
if f is None:
|
175 |
+
return None, "No file uploaded."
|
176 |
+
try:
|
177 |
+
return Image.open(f.name), get_file_info(f)
|
178 |
+
except Exception as e:
|
179 |
+
return None, f"Error loading image: {str(e)}"
|
180 |
+
|
181 |
+
|
182 |
+
with gr.Blocks(title="IMAGER ___ v0.2 Image Processing Tool") as demo:
|
183 |
gr.Markdown("# 🖼️ IMAGER v0.2 - Image Processing Tool 🛠️")
|
184 |
gr.Markdown("📤 Upload an image and select one of the processing options below to get started! 🚀")
|
185 |
+
|
186 |
+
with gr.Row(equal_height=True):
|
187 |
with gr.Column(scale=1):
|
188 |
+
# Image Upload Section
|
189 |
+
with gr.Group():
|
190 |
+
input_image = gr.Image(type="pil", label="Image Preview", scale=2)
|
191 |
+
input_file = gr.File(label="Upload Image", scale=1)
|
192 |
+
input_info = gr.HTML(label="Image Information")
|
193 |
|
194 |
+
# Basic Settings Section
|
195 |
+
with gr.Group():
|
196 |
+
gr.Markdown("### Settings")
|
197 |
+
settings = gr.Radio(
|
198 |
+
choices=["None", "S light", "M light", "L light", "S dark", "M dark", "L dark"],
|
199 |
+
label="Preset Settings",
|
200 |
+
value="None",
|
201 |
+
container=True
|
202 |
+
)
|
203 |
+
with gr.Row():
|
204 |
+
crop = gr.Checkbox(label="Crop", container=True)
|
205 |
+
remove_bg = gr.Checkbox(label="Remove Background", container=True)
|
206 |
+
|
207 |
+
# Size and Background Settings
|
208 |
+
with gr.Row():
|
209 |
+
resize = gr.Textbox(
|
210 |
+
label="Resize (WxH)",
|
211 |
+
placeholder="Example: 100x100",
|
212 |
+
container=True
|
213 |
+
)
|
214 |
+
padding = gr.Slider(
|
215 |
+
minimum=0,
|
216 |
+
maximum=200,
|
217 |
+
label="Padding",
|
218 |
+
container=True
|
219 |
+
)
|
220 |
+
background = gr.Textbox(
|
221 |
+
label="Background",
|
222 |
+
placeholder="Color name or hex code",
|
223 |
+
container=True
|
224 |
+
)
|
225 |
+
quality = gr.Slider(
|
226 |
+
minimum=1,
|
227 |
+
maximum=95,
|
228 |
+
value=85,
|
229 |
+
step=1,
|
230 |
+
label="Image Quality %",
|
231 |
+
container=True
|
232 |
+
)
|
233 |
+
|
234 |
+
# Output Section
|
235 |
with gr.Column(scale=1):
|
236 |
+
with gr.Group():
|
237 |
+
gr.Markdown("### Preview and Downloads")
|
238 |
+
output_image = gr.Image(type="pil", label="Processed Image Preview")
|
239 |
+
settings_info = gr.HTML(label="Applied Settings")
|
240 |
+
|
241 |
+
with gr.Row():
|
242 |
+
webp_download = gr.File(label="WebP")
|
243 |
+
png_download = gr.File(label="PNG")
|
244 |
+
jpg_download = gr.File(label="JPG")
|
245 |
+
|
246 |
+
# Process Button
|
247 |
+
with gr.Row():
|
248 |
+
process_btn = gr.Button("Process Image", size="lg", variant="primary")
|
249 |
+
|
250 |
+
# Examples Section
|
251 |
+
with gr.Accordion("Examples", open=False):
|
252 |
+
gr.Examples(
|
253 |
+
examples=example_images,
|
254 |
+
inputs=[
|
255 |
+
input_file, settings, crop, remove_bg, resize,
|
256 |
+
padding, background, quality
|
257 |
+
]
|
258 |
+
)
|
259 |
+
|
260 |
+
# Wire up the events
|
261 |
+
input_file.change(
|
262 |
+
fn=preview_and_info,
|
263 |
+
inputs=[input_file],
|
264 |
+
outputs=[input_image, input_info]
|
265 |
+
)
|
266 |
+
|
267 |
process_btn.click(
|
268 |
fn=gradio_interface,
|
269 |
inputs=[
|
270 |
+
input_file, settings, crop, remove_bg, resize,
|
271 |
padding, background, quality
|
272 |
],
|
273 |
outputs=[
|
|
|
275 |
]
|
276 |
)
|
277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
if __name__ == "__main__":
|
279 |
+
demo.launch()
|
gradio_test.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import os
|
4 |
+
import io
|
5 |
+
|
6 |
+
def get_file_info(file):
|
7 |
+
"""Get file info from the uploaded file."""
|
8 |
+
if file is None:
|
9 |
+
return "No file uploaded."
|
10 |
+
|
11 |
+
# Extract original filename
|
12 |
+
original_name = os.path.splitext(os.path.basename(file.name))[0]
|
13 |
+
|
14 |
+
# Open the image file
|
15 |
+
image = Image.open(file.name)
|
16 |
+
|
17 |
+
# Get image dimensions
|
18 |
+
width, height = image.size
|
19 |
+
|
20 |
+
# Calculate file size
|
21 |
+
size_bytes = os.path.getsize(file.name)
|
22 |
+
size_kb = size_bytes / 1024
|
23 |
+
|
24 |
+
return f"""<div style="text-align: left; padding: 5px;">
|
25 |
+
<p>Original Name: {original_name}</p>
|
26 |
+
<p>Size: {width}x{height}px ({size_kb:.1f} KB)</p>
|
27 |
+
</div>"""
|
28 |
+
|
29 |
+
with gr.Blocks(title="Gradio File Info Test") as interface:
|
30 |
+
gr.Markdown("# 🖼️ Gradio File Info Test 🛠️")
|
31 |
+
gr.Markdown("📤 Upload an image to test file info handling.")
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
with gr.Column(scale=1):
|
35 |
+
# Input column
|
36 |
+
input_file = gr.File(label="Upload Image")
|
37 |
+
input_info = gr.HTML(label="Input Image Information") # For showing input image info
|
38 |
+
|
39 |
+
# Update image info when file changes
|
40 |
+
input_file.change(
|
41 |
+
fn=get_file_info,
|
42 |
+
inputs=[input_file],
|
43 |
+
outputs=[input_info]
|
44 |
+
)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
interface.launch()
|