T.Masuda commited on
Commit
cb36b0b
·
1 Parent(s): 3699727

update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -2,15 +2,25 @@ import gradio as gr
2
  from rembg import remove
3
  from PIL import ImageOps, ImageDraw
4
 
5
- def process_image(foreImg, flip, backImg, left, top, width, height, isLoc):
6
  if foreImg is None:
7
  return None
8
 
 
9
  imwidth, imheight = foreImg.size
10
- if width > 0:
11
- imwidth = width
12
- if height > 0:
13
- imheight = height
 
 
 
 
 
 
 
 
 
14
 
15
  if isLoc:
16
  if backImg is None:
@@ -39,6 +49,9 @@ app = gr.Interface(
39
  gr.Image(label='background', type='pil'),
40
  gr.Slider(maximum=4000, step=1, label='left'),
41
  gr.Slider(maximum=4000, step=1, label='top'),
 
 
 
42
  gr.Slider(maximum=4000, step=1, label='width'),
43
  gr.Slider(maximum=4000, step=1, label='height'),
44
  gr.Checkbox(label='check location only'),
@@ -46,7 +59,7 @@ app = gr.Interface(
46
  outputs='image',
47
  allow_flagging='never',
48
  concurrency_limit=20,
49
- examples=[['examples/foreground.jpg', False, 'examples/background.jpg', 720, 540, 256, 256, False]],
50
  #cache_examples=False
51
  )
52
  app.launch()
 
2
  from rembg import remove
3
  from PIL import ImageOps, ImageDraw
4
 
5
+ def process_image(foreImg, flip, backImg, left, top, method, hscale, vscale, width, height, isLoc):
6
  if foreImg is None:
7
  return None
8
 
9
+ bgwidth, bgheight = backImg.size
10
  imwidth, imheight = foreImg.size
11
+ if method == 'horizontal scale factor':
12
+ scale = bgwidth * hscale / imwidth
13
+ imwidth = int(imwidth * scale)
14
+ imheight = int(imheight * scale)
15
+ elif method == 'vertical scale factor':
16
+ scale = bgheight * vscale / imheight
17
+ imwidth = int(imwidth * scale)
18
+ imheight = int(imheight * scale)
19
+ else:
20
+ if width > 0:
21
+ imwidth = width
22
+ if height > 0:
23
+ imheight = height
24
 
25
  if isLoc:
26
  if backImg is None:
 
49
  gr.Image(label='background', type='pil'),
50
  gr.Slider(maximum=4000, step=1, label='left'),
51
  gr.Slider(maximum=4000, step=1, label='top'),
52
+ gr.Radio(['horizontal scale factor', 'vertical scale factor', 'size (width and height)'], label='size specification method', value='horizontal scale factor'),
53
+ gr.Slider(minimum=0.01, maximum=1.0, step=0.01, label='horizontal scale factor', value=0.5),
54
+ gr.Slider(minimum=0.01, maximum=1.0, step=0.01, label='vertical scale factor', value=0.5),
55
  gr.Slider(maximum=4000, step=1, label='width'),
56
  gr.Slider(maximum=4000, step=1, label='height'),
57
  gr.Checkbox(label='check location only'),
 
59
  outputs='image',
60
  allow_flagging='never',
61
  concurrency_limit=20,
62
+ examples=[['examples/foreground.jpg', False, 'examples/background.jpg', 720, 540, 'size (width and height)', 0.5, 0.5, 256, 256, False]],
63
  #cache_examples=False
64
  )
65
  app.launch()