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

Update SDK version to 5.1.0, refactor image handling in app.py and added examples folder

Browse files
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🎨
4
  colorFrom: gray
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 4.42.0
8
  app_file: app.py
9
  pinned: true
10
  license: mit
 
4
  colorFrom: gray
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 5.1.0
8
  app_file: app.py
9
  pinned: true
10
  license: mit
app.py CHANGED
@@ -251,14 +251,13 @@ def hex_to_rgba(hex_color):
251
 
252
  def apply_bg_image(image, background_file=None, background_color=None, bg_type="Color"):
253
  try:
254
- image_data = image.read()
255
- input_image = Image.open(io.BytesIO(image_data))
256
  origin = input_image.copy()
257
 
258
  color_profile = input_image.info.get("icc_profile")
259
 
260
  if background_file is not None:
261
- background_image = Image.open(io.BytesIO(background_file.read()))
262
  else:
263
  background_image = None
264
 
@@ -275,24 +274,20 @@ def apply_bg_image(image, background_file=None, background_color=None, bg_type="
275
  output_frame = apply_background(frame, background_image)
276
  frames.append(output_frame)
277
 
278
- output_image = io.BytesIO()
279
- frames[0].save(
280
- output_image,
281
  format="GIF",
282
  save_all=True,
283
  append_images=frames[1:],
284
  loop=0,
285
  icc_profile=color_profile,
286
  )
287
- output_image_base64 = base64.b64encode(output_image.getvalue()).decode("utf-8")
288
  else:
289
  output_image = apply_background(input_image, background_image)
290
- buffered = io.BytesIO()
291
- output_image.save(buffered, format="PNG", optimize=True, icc_profile=color_profile)
292
- output_image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
293
 
294
- output_image_data = base64.b64decode(output_image_base64)
295
- return (Image.open(io.BytesIO(output_image_data)), origin)
296
  except Exception as e:
297
  return str(e)
298
 
@@ -311,13 +306,13 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
311
  examples = gr.Examples(
312
  [
313
  load_img(
314
- "https://images.rawpixel.com/image_800/cHJpdmF0ZS9sci9pbWFnZXMvd2Vic2l0ZS8yMDIzLTA4L3Jhd3BpeGVsX29mZmljZV8yX3Bob3RvX29mX2FfbGlvbl9pc29sYXRlZF9vbl9jb2xvcl9iYWNrZ3JvdW5kXzJhNzgwMjM1LWRlYTgtNDMyOS04OWVjLTY3ZWMwNjcxZDhiMV8xLmpwZw.jpg",
315
  output_type="pil",
316
  )
317
  ],
318
  inputs=image_input,
319
- fn=remove_bg_fn,
320
  outputs=slider,
 
321
  cache_examples=True,
322
  cache_mode="eager",
323
  )
@@ -376,17 +371,12 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
376
 
377
  examples = gr.Examples(
378
  [
379
- ["https://pngimg.com/d/mario_PNG125.png", None, "#0cfa38", "Color"],
380
- [
381
- "https://pngimg.com/d/mario_PNG125.png",
382
- "https://cdn.photoroom.com/v2/image-cache?path=gs://background-7ef44.appspot.com/backgrounds_v3/black/47_-_black.jpg",
383
- None,
384
- "Image",
385
- ],
386
  ],
387
  inputs=[image_input, bg_image, color_picker, bg_type],
388
- fn=apply_bg_image,
389
  outputs=slider,
 
390
  cache_examples=True,
391
  cache_mode="eager",
392
  )
@@ -494,18 +484,18 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
494
  examples = gr.Examples(
495
  [
496
  [
497
- "https://www.w3schools.com/html/mov_bbb.mp4",
498
  "Video",
499
  None,
500
- "https://www.w3schools.com/howto/rain.mp4",
501
  ],
502
  [
503
- "https://www.w3schools.com/html/mov_bbb.mp4",
504
  "Image",
505
- "https://cdn.photoroom.com/v2/image-cache?path=gs://background-7ef44.appspot.com/backgrounds_v3/black/47_-_black.jpg",
506
  None,
507
  ],
508
- ["https://www.w3schools.com/html/mov_bbb.mp4", "Color", None, None],
509
  ],
510
  inputs=[in_video, bg_type, bg_image, bg_video],
511
  outputs=[stream_image, out_video, time_textbox],
 
251
 
252
  def apply_bg_image(image, background_file=None, background_color=None, bg_type="Color"):
253
  try:
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:
260
+ background_image = load_img(background_file, output_type="pil")
261
  else:
262
  background_image = None
263
 
 
274
  output_frame = apply_background(frame, background_image)
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
 
 
306
  examples = gr.Examples(
307
  [
308
  load_img(
309
+ "./examples/lion.jpg",
310
  output_type="pil",
311
  )
312
  ],
313
  inputs=image_input,
 
314
  outputs=slider,
315
+ fn=remove_bg_fn,
316
  cache_examples=True,
317
  cache_mode="eager",
318
  )
 
371
 
372
  examples = gr.Examples(
373
  [
374
+ ["./examples/mario.png", None, "#0cfa38", "Color"],
375
+ ["./examples/mario.png", "./examples/bg-image.jpg", None, "Image"],
 
 
 
 
 
376
  ],
377
  inputs=[image_input, bg_image, color_picker, bg_type],
 
378
  outputs=slider,
379
+ fn=apply_bg_image,
380
  cache_examples=True,
381
  cache_mode="eager",
382
  )
 
484
  examples = gr.Examples(
485
  [
486
  [
487
+ "./examples/videoplayback.mp4",
488
  "Video",
489
  None,
490
+ "./examples/mov_bbb.mp4",
491
  ],
492
  [
493
+ "./examples/videoplayback.mp4",
494
  "Image",
495
+ "./examples/bg-image.jpg",
496
  None,
497
  ],
498
+ ["./examples/videoplayback.mp4", "Color", None, None],
499
  ],
500
  inputs=[in_video, bg_type, bg_image, bg_video],
501
  outputs=[stream_image, out_video, time_textbox],
examples/bg-image.jpg ADDED
examples/lion.jpg ADDED
examples/mario.png ADDED
examples/mov_bbb.mp4 ADDED
Binary file (788 kB). View file
 
examples/videoplayback.mp4 ADDED
Binary file (133 kB). View file
 
requirements.txt CHANGED
@@ -11,7 +11,7 @@ typing
11
  scikit-image
12
  huggingface_hub
13
  transformers>=4.39.1
14
- gradio==4.42.0
15
  schedule
16
  loadimg>=0.1.1
17
  moviepy==1.0.3
 
11
  scikit-image
12
  huggingface_hub
13
  transformers>=4.39.1
14
+ gradio==5.1.0
15
  schedule
16
  loadimg>=0.1.1
17
  moviepy==1.0.3