woodmastr commited on
Commit
908cad0
1 Parent(s): a6e0ade

Update gradio_imager.py

Browse files
Files changed (1) hide show
  1. gradio_imager.py +21 -20
gradio_imager.py CHANGED
@@ -11,9 +11,9 @@ def apply_standard_settings(setting):
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))
@@ -56,33 +56,33 @@ def gradio_interface(image, standard_settings, crop=False, remove_bg=False, resi
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:
62
- image.save(tmp_input, format="PNG")
63
- tmp_input_path = tmp_input.name
64
-
65
- # Prepare a temporary file for the output image
66
- tmp_output_path = tempfile.mktemp(suffix=".png")
67
 
68
- # Process the image
69
- process_image(tmp_input_path, tmp_output_path, crop,
70
- remove_bg, resize_dimensions, padding, background)
71
 
72
- # Load and return the processed image
73
- processed_image = Image.open(tmp_output_path)
74
 
75
- # Clean up temporary files
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"),
 
86
  gr.components.Radio(choices=[
87
  "None", "S light", "M light", "L light", "S dark", "M dark", "L dark"], label="Settings"),
88
  gr.components.Checkbox(label="Crop"),
@@ -98,6 +98,7 @@ interface = gr.Interface(fn=gradio_interface,
98
  gr.components.Image(type="pil"),
99
  gr.components.HTML(label="Applied Settings")
100
  ],
 
101
  title="IMAGER ___ Image Processor",
102
  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.",)
103
 
 
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, "#2A373D"),
15
+ "M dark": (True, True, "480x480", 96, "#2A373D"),
16
+ "L dark": (True, True, "960x960", 128, "#2A373D"),
17
  }
18
  # Default to no special settings
19
  return settings_dict.get(setting, (None, None, None, None, None))
 
56
  resize_dimensions = (width, height)
57
  except ValueError:
58
  return "Invalid format for resize dimensions. Please use 'WxH'.", "original", applied_settings
59
+ # Process the image directly
60
+ processed_image = process_image(
61
+ image, crop, remove_bg, resize_dimensions, padding, background)
62
 
63
+ # Generate settings description
64
+ applied_settings = settings_description(
65
+ crop, remove_bg, resize, padding, background)
 
 
 
 
66
 
67
+ return processed_image, applied_settings
 
 
68
 
 
 
69
 
70
+ example_images = [
71
+ [os.path.join("data", "examples", "supermario.png"),
72
+ "S light", True, True, "480x420", 10, "whitesmoke"],
73
+ [os.path.join("data", "examples",
74
+ "depositphotos_520707962-stock-photo-fujifilm-s10-body-black-fujifilm.jpg"), "None", True, True, "480x320", 48, "blue"],
75
+ [os.path.join("data", "examples", "batman_b_c_320x280_bg.png"),
76
+ "None", True, True, "360x360", 48, "yellow"],
77
 
 
78
 
79
+ ]
80
 
81
  # Define the Gradio interface
82
  interface = gr.Interface(fn=gradio_interface,
83
  inputs=[
84
+ gr.components.Image(
85
+ type="pil", label="Input Image"),
86
  gr.components.Radio(choices=[
87
  "None", "S light", "M light", "L light", "S dark", "M dark", "L dark"], label="Settings"),
88
  gr.components.Checkbox(label="Crop"),
 
98
  gr.components.Image(type="pil"),
99
  gr.components.HTML(label="Applied Settings")
100
  ],
101
+ examples=example_images,
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