codelion commited on
Commit
a746632
·
verified ·
1 Parent(s): 392146d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -54,7 +54,7 @@ def generate_html_snippet(image_path_prefix):
54
 
55
  return html
56
 
57
- def process_image(input_img):
58
  """Main processing function"""
59
  if input_img is None:
60
  return None, None, "Please upload an image"
@@ -65,23 +65,14 @@ def process_image(input_img):
65
  processed_paths = [] # Initialize the list outside try block
66
 
67
  try:
68
- # Get original filename
69
- base_filename = "image" # Default filename
70
- if isinstance(input_img, dict) and "name" in input_img:
71
- original_name = input_img["name"]
72
- base_filename = os.path.splitext(original_name)[0]
73
 
74
- # Get image data
75
- if isinstance(input_img, dict) and "image" in input_img:
76
- img_data = input_img["image"]
77
- else:
78
- img_data = input_img
79
-
80
  # Convert to PIL Image if needed
81
- if isinstance(img_data, np.ndarray):
82
- img = Image.fromarray(img_data)
83
  else:
84
- img = img_data
85
 
86
  # Convert to RGB if necessary
87
  if img.mode != 'RGB':
@@ -138,10 +129,17 @@ with gr.Blocks(title="Responsive Image Generator") as app:
138
 
139
  with gr.Row():
140
  with gr.Column():
141
- input_image = gr.Image(
142
- label="Upload Original Image",
143
- type="pil" # Changed to PIL type
144
- )
 
 
 
 
 
 
 
145
  process_btn = gr.Button("Process Image")
146
 
147
  with gr.Column():
@@ -152,9 +150,23 @@ with gr.Blocks(title="Responsive Image Generator") as app:
152
  )
153
  output_message = gr.Textbox(label="Status")
154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  process_btn.click(
156
  fn=process_image,
157
- inputs=[input_image], # Now only passing single input
158
  outputs=[output_zip, output_html, output_message]
159
  )
160
 
 
54
 
55
  return html
56
 
57
+ def process_image(input_img, input_name):
58
  """Main processing function"""
59
  if input_img is None:
60
  return None, None, "Please upload an image"
 
65
  processed_paths = [] # Initialize the list outside try block
66
 
67
  try:
68
+ # Get original filename without extension
69
+ base_filename = os.path.splitext(input_name)[0] if input_name else "image"
 
 
 
70
 
 
 
 
 
 
 
71
  # Convert to PIL Image if needed
72
+ if isinstance(input_img, np.ndarray):
73
+ img = Image.fromarray(input_img)
74
  else:
75
+ img = input_img
76
 
77
  # Convert to RGB if necessary
78
  if img.mode != 'RGB':
 
129
 
130
  with gr.Row():
131
  with gr.Column():
132
+ with gr.Row():
133
+ input_image = gr.Image(
134
+ label="Upload Original Image",
135
+ type="pil",
136
+ show_label=True
137
+ )
138
+ with gr.Row():
139
+ input_name = gr.Textbox(
140
+ label="Original Filename (optional)",
141
+ placeholder="Enter filename or leave empty to use uploaded filename"
142
+ )
143
  process_btn = gr.Button("Process Image")
144
 
145
  with gr.Column():
 
150
  )
151
  output_message = gr.Textbox(label="Status")
152
 
153
+ def handle_upload(img):
154
+ """Handle image upload to get filename"""
155
+ if isinstance(img, dict) and 'name' in img:
156
+ return img['name']
157
+ return ""
158
+
159
+ # Update filename when image is uploaded
160
+ input_image.upload(
161
+ fn=handle_upload,
162
+ inputs=[input_image],
163
+ outputs=[input_name]
164
+ )
165
+
166
+ # Process button click
167
  process_btn.click(
168
  fn=process_image,
169
+ inputs=[input_image, input_name],
170
  outputs=[output_zip, output_html, output_message]
171
  )
172