Spaces:
fantos
/
Runtime error

arxivgpt kim commited on
Commit
d975af1
ยท
verified ยท
1 Parent(s): cffeaa2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -82,17 +82,25 @@ def calculate_position(org_size, add_size, position):
82
  elif position == "ํ•˜๋‹จ ์šฐ์ธก":
83
  return (org_size[0] - add_size[0], org_size[1] - add_size[1])
84
 
85
- def merge(org_image, add_image, scale, position):
 
 
 
 
 
86
  scale_percentage = scale / 100.0
87
  new_size = (int(add_image.width * scale_percentage), int(add_image.height * scale_percentage))
88
  add_image = add_image.resize(new_size, Image.Resampling.LANCZOS)
89
 
90
  position = calculate_position(org_image.size, add_image.size, position)
91
- org_image.paste(add_image, position, add_image)
 
 
92
 
93
- return org_image
94
-
95
-
 
96
 
97
  with gr.Blocks() as demo:
98
  with gr.Tab("Background Removal"):
@@ -111,12 +119,18 @@ with gr.Blocks() as demo:
111
 
112
  with gr.Tab("Merge"):
113
  with gr.Column():
114
- org_image = gr.Image(label="Background", type='pil', image_mode='RGBA', height="40vh")
115
- add_image = gr.Image(label="Foreground", type='pil', image_mode='RGBA', height="40vh")
116
  scale = gr.Slider(minimum=10, maximum=200, step=1, value=100, label="Scale of Foreground Image (%)")
117
  position = gr.Radio(choices=["์ค‘์•™ ๊ฐ€์šด๋ฐ", "์ƒ๋‹จ ์ขŒ์ธก", "์ƒ๋‹จ ๊ฐ€์šด๋ฐ", "์ƒ๋‹จ ์šฐ์ธก", "์ค‘์•™ ์ขŒ์ธก", "์ค‘์•™ ์šฐ์ธก", "ํ•˜๋‹จ ์ขŒ์ธก", "ํ•˜๋‹จ ๊ฐ€์šด๋ฐ", "ํ•˜๋‹จ ์šฐ์ธก"], value="์ค‘์•™ ๊ฐ€์šด๋ฐ", label="Position of Foreground Image")
 
118
  btn_merge = gr.Button("Merge Images")
119
- result_merge = gr.Image(height="40vh")
120
- btn_merge.click(fn=merge, inputs=[org_image, add_image, scale, position], outputs=result_merge)
 
 
 
 
 
121
 
122
  demo.launch()
 
82
  elif position == "ํ•˜๋‹จ ์šฐ์ธก":
83
  return (org_size[0] - add_size[0], org_size[1] - add_size[1])
84
 
85
+
86
+ def merge(org_image, add_image, scale, position, display_size):
87
+ # ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ๋””์Šคํ”Œ๋ ˆ์ด ํฌ๊ธฐ์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์กฐ์ ˆ
88
+ display_width, display_height = map(int, display_size.split('x'))
89
+
90
+ # ์ด๋ฏธ์ง€ ๋ณ‘ํ•ฉ ๋กœ์ง
91
  scale_percentage = scale / 100.0
92
  new_size = (int(add_image.width * scale_percentage), int(add_image.height * scale_percentage))
93
  add_image = add_image.resize(new_size, Image.Resampling.LANCZOS)
94
 
95
  position = calculate_position(org_image.size, add_image.size, position)
96
+ merged_image = Image.new("RGBA", org_image.size)
97
+ merged_image.paste(org_image, (0, 0))
98
+ merged_image.paste(add_image, position, add_image)
99
 
100
+ # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€ ๋””์Šคํ”Œ๋ ˆ์ด ํฌ๊ธฐ ์กฐ์ ˆ
101
+ final_image = merged_image.resize((display_width, display_height), Image.Resampling.LANCZOS)
102
+
103
+ return final_image
104
 
105
  with gr.Blocks() as demo:
106
  with gr.Tab("Background Removal"):
 
119
 
120
  with gr.Tab("Merge"):
121
  with gr.Column():
122
+ org_image = gr.Image(label="Background", type='pil', image_mode='RGBA', height=400) # ์˜ˆ์‹œ๋กœ ๋†’์ด ์กฐ์ ˆ
123
+ add_image = gr.Image(label="Foreground", type='pil', image_mode='RGBA', height=400) # ์˜ˆ์‹œ๋กœ ๋†’์ด ์กฐ์ ˆ
124
  scale = gr.Slider(minimum=10, maximum=200, step=1, value=100, label="Scale of Foreground Image (%)")
125
  position = gr.Radio(choices=["์ค‘์•™ ๊ฐ€์šด๋ฐ", "์ƒ๋‹จ ์ขŒ์ธก", "์ƒ๋‹จ ๊ฐ€์šด๋ฐ", "์ƒ๋‹จ ์šฐ์ธก", "์ค‘์•™ ์ขŒ์ธก", "์ค‘์•™ ์šฐ์ธก", "ํ•˜๋‹จ ์ขŒ์ธก", "ํ•˜๋‹จ ๊ฐ€์šด๋ฐ", "ํ•˜๋‹จ ์šฐ์ธก"], value="์ค‘์•™ ๊ฐ€์šด๋ฐ", label="Position of Foreground Image")
126
+ display_size = gr.Textbox(value="800x600", label="Display Size (Width x Height)")
127
  btn_merge = gr.Button("Merge Images")
128
+ result_merge = gr.Image()
129
+
130
+ btn_merge.click(
131
+ fn=merge,
132
+ inputs=[org_image, add_image, scale, position, display_size],
133
+ outputs=result_merge,
134
+ )
135
 
136
  demo.launch()