woodmastr commited on
Commit
1e4ba81
1 Parent(s): b690518

Update gradio_imager.py

Browse files
Files changed (1) hide show
  1. gradio_imager.py +57 -11
gradio_imager.py CHANGED
@@ -5,7 +5,49 @@ import os
5
  from image_processor import process_image
6
 
7
 
8
- def gradio_interface(image, crop, remove_bg, resize, padding, background):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  # Convert resize string to tuple (if provided)
10
  resize_dimensions = None
11
  if resize:
@@ -13,7 +55,7 @@ def gradio_interface(image, crop, remove_bg, resize, padding, background):
13
  width, height = map(int, resize.split('x'))
14
  resize_dimensions = (width, height)
15
  except ValueError:
16
- return "Invalid format for resize dimensions. Please use 'AxB'.", "original"
17
 
18
  # Use a temporary file to save the input image from Gradio
19
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_input:
@@ -34,16 +76,18 @@ def gradio_interface(image, crop, remove_bg, resize, padding, background):
34
  os.remove(tmp_input_path)
35
  os.remove(tmp_output_path)
36
 
37
- return processed_image
38
 
39
 
40
- # Define the Gradio interface with updated component imports
41
  interface = gr.Interface(fn=gradio_interface,
42
  inputs=[
43
- gr.components.Image(type="pil"),
 
 
 
44
  gr.components.Checkbox(label="Crop"),
45
- gr.components.Checkbox(
46
- label="Remove Background"),
47
  gr.components.Textbox(
48
  label="Resize (WxH)", placeholder="Example: 100x100"),
49
  gr.components.Slider(
@@ -51,10 +95,12 @@ interface = gr.Interface(fn=gradio_interface,
51
  gr.components.Textbox(
52
  label="Background", placeholder="Color name or hex code")
53
  ],
54
- outputs=gr.components.Image(type="pil"),
55
- title="Image Processor",
56
- description="Upload an image and select processing options.")
57
-
 
 
58
 
59
  if __name__ == "__main__":
60
  interface.launch()
 
5
  from image_processor import process_image
6
 
7
 
8
+ def apply_standard_settings(setting):
9
+ """Returns the parameters for the selected standard setting."""
10
+ settings_dict = {
11
+ "S light": (True, True, "240x240", 48, "whitesmoke"),
12
+ "M light": (True, True, "480x480", 96, "whitesmoke"),
13
+ "L light": (True, True, "960x960", 128, "whitesmoke"),
14
+ "S dark": (True, True, "240x240", 48, "dimgray"),
15
+ "M dark": (True, True, "480x480", 96, "dimgray"),
16
+ "L dark": (True, True, "960x960", 128, "dimgray"),
17
+ }
18
+ # Default to no special settings
19
+ return settings_dict.get(setting, (None, None, None, None, None))
20
+
21
+
22
+ def settings_description(crop, remove_bg, resize, padding, background):
23
+ """Generate an HTML text description of the current settings in a smaller font and list format."""
24
+ description = f"""
25
+ <ul style="font-size:small;">
26
+ <li>Crop: {crop}</li>
27
+ <li>Remove Background: {remove_bg}</li>
28
+ <li>Resize: {resize if resize else 'No resize'}</li>
29
+ <li>Padding: {padding}</li>
30
+ <li>Background: {background}</li>
31
+ </ul>
32
+ """
33
+ return description
34
+
35
+
36
+ def gradio_interface(image, standard_settings, crop=False, remove_bg=False, resize=None, padding=0, background="white"):
37
+ # Apply standard settings if selected and not "None"
38
+ if image is None:
39
+ # Load the standard image from the specified path if no image is uploaded
40
+ standard_image_path = './data/examples/supermario.png'
41
+ image = Image.open(standard_image_path)
42
+
43
+ if standard_settings and standard_settings != "None":
44
+ crop, remove_bg, resize, padding, background = apply_standard_settings(
45
+ standard_settings)
46
+
47
+ # Generate settings description
48
+ applied_settings = settings_description(
49
+ crop, remove_bg, resize, padding, background)
50
+
51
  # Convert resize string to tuple (if provided)
52
  resize_dimensions = None
53
  if resize:
 
55
  width, height = map(int, resize.split('x'))
56
  resize_dimensions = (width, height)
57
  except ValueError:
58
+ return "Invalid format for resize dimensions. Please use 'WxH'.", "original", applied_settings
59
 
60
  # Use a temporary file to save the input image from Gradio
61
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_input:
 
76
  os.remove(tmp_input_path)
77
  os.remove(tmp_output_path)
78
 
79
+ return processed_image, applied_settings
80
 
81
 
82
+ # Define the Gradio interface
83
  interface = gr.Interface(fn=gradio_interface,
84
  inputs=[
85
+ gr.components.Image(type="pil", examples=[
86
+ "data/examples/supermario.png"]),
87
+ gr.components.Radio(choices=[
88
+ "None", "S light", "M light", "L light", "S dark", "M dark", "L dark"], label="Settings"),
89
  gr.components.Checkbox(label="Crop"),
90
+ gr.components.Checkbox(label="Remove Background"),
 
91
  gr.components.Textbox(
92
  label="Resize (WxH)", placeholder="Example: 100x100"),
93
  gr.components.Slider(
 
95
  gr.components.Textbox(
96
  label="Background", placeholder="Color name or hex code")
97
  ],
98
+ outputs=[
99
+ gr.components.Image(type="pil"),
100
+ gr.components.HTML(label="Applied Settings")
101
+ ],
102
+ title="IMAGER ___ Image Processor",
103
+ description="Upload an image and select processing options or choose a standard setting. Supports crop, autoremove background, resize, add padding, and set the background color.",)
104
 
105
  if __name__ == "__main__":
106
  interface.launch()