Spaces:
fantos
/
Runtime error

arxivgpt kim commited on
Commit
ca70732
·
verified ·
1 Parent(s): 101fa6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -2
app.py CHANGED
@@ -102,5 +102,54 @@ examples = [['./input.jpg'],]
102
  # demo = gr.Interface(fn=process,inputs="image", outputs=output, examples=examples, title=title, description=description)
103
  demo = gr.Interface(fn=process,inputs="image", outputs="image", examples=examples, title=title, description=description)
104
 
105
- if __name__ == "__main__":
106
- demo.launch(share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  # demo = gr.Interface(fn=process,inputs="image", outputs=output, examples=examples, title=title, description=description)
103
  demo = gr.Interface(fn=process,inputs="image", outputs="image", examples=examples, title=title, description=description)
104
 
105
+
106
+ def merge_images(background_path, foreground_image):
107
+ """
108
+ 배경 이미지에 배경이 제거된 이미지를 투명하게 삽입합니다.
109
+ 배경이 제거된 이미지는 배경 이미지 중앙에 30% 크기로 축소되어 삽입됩니다.
110
+ """
111
+ # 배경 이미지 로드
112
+ background = Image.open(background_path).convert("RGBA")
113
+ # 전경(배경이 제거된 이미지) 이미지 로드
114
+ foreground = foreground_image.convert("RGBA")
115
+
116
+ # 전경 이미지를 배경 이미지의 30% 크기로 조정
117
+ scale_factor = 0.3
118
+ new_size = (int(background.width * scale_factor), int(foreground.height * background.width / foreground.width * scale_factor))
119
+ foreground_resized = foreground.resize(new_size, Image.Resampling.LANCZOS)
120
+
121
+ # 전경 이미지를 배경 이미지의 가운데에 위치시키기 위한 좌표 계산
122
+ x = (background.width - new_size[0]) // 2
123
+ y = (background.height - new_size[1]) // 2
124
+
125
+ # 배경 이미지 위에 전경 이미지를 붙임
126
+ background.paste(foreground_resized, (x, y), foreground_resized)
127
+
128
+ return background
129
+
130
+ # process 함수 수정
131
+ def process(image, background_image=None):
132
+ """
133
+ 이미지에서 배경을 제거하고, 선택적으로 배경 이미지 위에 배치합니다.
134
+ """
135
+ # 기존 배경 제거 로직...
136
+ # 배경 이미지가 제공된 경우, 배경 이미지에 배경이 제거된 이미지를 삽입
137
+ if background_image is not None:
138
+ result_image = merge_images(background_image, new_im)
139
+ else:
140
+ result_image = new_im
141
+
142
+ return result_image
143
+
144
+ # Gradio 인터페이스 수정
145
+ demo = gr.Interface(
146
+ fn=process,
147
+ inputs=[
148
+ gr.Image(type="pil", label="Image to remove background"),
149
+ gr.Image(type="pil", label="Optional: Background Image", optional=True)
150
+ ],
151
+ outputs="image",
152
+ examples=examples,
153
+ title=title,
154
+ description=description
155
+ )