Adeal1 commited on
Commit
989079a
·
1 Parent(s): 36b846f

Refactor image processing to save temporary files for original and processed images in PNG and GIF formats

Browse files
Files changed (1) hide show
  1. app.py +38 -18
app.py CHANGED
@@ -194,6 +194,10 @@ def remove_bg_fn(image):
194
  im = im.convert("RGB")
195
  origin = im.copy()
196
 
 
 
 
 
197
  if im.format == "GIF":
198
  frames = []
199
  for frame in ImageSequence.Iterator(im):
@@ -201,17 +205,22 @@ def remove_bg_fn(image):
201
  processed_frame = process_image(frame)
202
  frames.append(processed_frame)
203
  processed_image = frames[0]
204
- processed_image.save(
205
- io.BytesIO(),
206
- format="GIF",
207
- save_all=True,
208
- append_images=frames[1:],
209
- loop=0,
210
- )
 
 
211
  else:
212
  processed_image = process_image(im)
 
 
 
213
 
214
- return (processed_image, origin)
215
 
216
  @spaces.GPU
217
  def process_image(image):
@@ -254,6 +263,10 @@ def apply_bg_image(image, background_file=None, background_color=None, bg_type="
254
  input_image = load_img(image, output_type="pil")
255
  origin = input_image.copy()
256
 
 
 
 
 
257
  color_profile = input_image.info.get("icc_profile")
258
 
259
  if background_file is not None:
@@ -275,19 +288,23 @@ def apply_bg_image(image, background_file=None, background_color=None, bg_type="
275
  frames.append(output_frame)
276
 
277
  output_image = frames[0]
278
- output_image.save(
279
- "output.gif",
280
- format="GIF",
281
- save_all=True,
282
- append_images=frames[1:],
283
- loop=0,
284
- icc_profile=color_profile,
285
- )
 
 
286
  else:
287
  output_image = apply_background(input_image, background_image)
288
- output_image.save("output.png", format="PNG", optimize=True, icc_profile=color_profile)
 
 
289
 
290
- return (output_image, origin)
291
  except Exception as e:
292
  return str(e)
293
 
@@ -454,6 +471,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
454
  gr.update(visible=True),
455
  gr.update(visible=False),
456
  gr.update(visible=False),
 
457
  )
458
  elif bg_type == "Image":
459
  return (
@@ -467,12 +485,14 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
467
  gr.update(visible=False),
468
  gr.update(visible=False),
469
  gr.update(visible=True),
 
470
  )
471
  else:
472
  return (
473
  gr.update(visible=False),
474
  gr.update(visible=False),
475
  gr.update(visible=False),
 
476
  )
477
 
478
  bg_type.change(
 
194
  im = im.convert("RGB")
195
  origin = im.copy()
196
 
197
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
198
+ origin_filepath = temp_file.name
199
+ origin.save(origin_filepath, format="PNG")
200
+
201
  if im.format == "GIF":
202
  frames = []
203
  for frame in ImageSequence.Iterator(im):
 
205
  processed_frame = process_image(frame)
206
  frames.append(processed_frame)
207
  processed_image = frames[0]
208
+ with tempfile.NamedTemporaryFile(suffix=".gif", delete=False) as temp_file:
209
+ temp_filepath = temp_file.name
210
+ processed_image.save(
211
+ temp_filepath,
212
+ format="GIF",
213
+ save_all=True,
214
+ append_images=frames[1:],
215
+ loop=0,
216
+ )
217
  else:
218
  processed_image = process_image(im)
219
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
220
+ temp_filepath = temp_file.name
221
+ processed_image.save(temp_filepath, format="PNG")
222
 
223
+ return (temp_filepath, origin_filepath)
224
 
225
  @spaces.GPU
226
  def process_image(image):
 
263
  input_image = load_img(image, output_type="pil")
264
  origin = input_image.copy()
265
 
266
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
267
+ origin_filepath = temp_file.name
268
+ origin.save(origin_filepath, format="PNG")
269
+
270
  color_profile = input_image.info.get("icc_profile")
271
 
272
  if background_file is not None:
 
288
  frames.append(output_frame)
289
 
290
  output_image = frames[0]
291
+ with tempfile.NamedTemporaryFile(suffix=".gif", delete=False) as temp_file:
292
+ temp_filepath = temp_file.name
293
+ output_image.save(
294
+ temp_filepath,
295
+ format="GIF",
296
+ save_all=True,
297
+ append_images=frames[1:],
298
+ loop=0,
299
+ icc_profile=color_profile,
300
+ )
301
  else:
302
  output_image = apply_background(input_image, background_image)
303
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
304
+ temp_filepath = temp_file.name
305
+ output_image.save(temp_filepath, format="PNG", optimize=True, icc_profile=color_profile)
306
 
307
+ return (temp_filepath, origin_filepath)
308
  except Exception as e:
309
  return str(e)
310
 
 
471
  gr.update(visible=True),
472
  gr.update(visible=False),
473
  gr.update(visible=False),
474
+ gr.update(visible=False),
475
  )
476
  elif bg_type == "Image":
477
  return (
 
485
  gr.update(visible=False),
486
  gr.update(visible=False),
487
  gr.update(visible=True),
488
+ gr.update(visible=True),
489
  )
490
  else:
491
  return (
492
  gr.update(visible=False),
493
  gr.update(visible=False),
494
  gr.update(visible=False),
495
+ gr.update(visible=False),
496
  )
497
 
498
  bg_type.change(