Upload folder using huggingface_hub
Browse files- image_styler_v2.py +32 -22
image_styler_v2.py
CHANGED
|
@@ -87,14 +87,28 @@ def enhance_unified_image(harmonized_img, enhancer):
|
|
| 87 |
|
| 88 |
return enhanced_img
|
| 89 |
|
| 90 |
-
def embed_person_on_background(person_img, background_img):
|
| 91 |
-
#
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# Create a new image with the same size as the background and paste the person image onto it
|
| 95 |
combined_img = Image.new("RGBA", background_img.size)
|
| 96 |
combined_img.paste(background_img, (0, 0))
|
| 97 |
-
combined_img.paste(person_img, (
|
| 98 |
|
| 99 |
return combined_img
|
| 100 |
|
|
@@ -131,11 +145,7 @@ def enhance_image(image, contrast, brightness, color):
|
|
| 131 |
enhanced_image = enhancer.enhance(factor)
|
| 132 |
return enhanced_image
|
| 133 |
|
| 134 |
-
def process_images(person_img, background_img,
|
| 135 |
-
# Validate parameters
|
| 136 |
-
if not (1 <= num_images <= 5):
|
| 137 |
-
raise ValueError("Number of Output Images must be between 1 and 5")
|
| 138 |
-
|
| 139 |
# Remove background from the person image
|
| 140 |
person_no_bg = remove(person_img)
|
| 141 |
|
|
@@ -146,7 +156,7 @@ def process_images(person_img, background_img, num_images, enhance, auto_match,
|
|
| 146 |
print(f"Applying enhancement with contrast={contrast}, brightness={brightness}, color={color}...")
|
| 147 |
person_no_bg = enhance_image(person_no_bg, contrast, brightness, color)
|
| 148 |
|
| 149 |
-
combined_img = embed_person_on_background(person_no_bg, background_img)
|
| 150 |
|
| 151 |
if unify:
|
| 152 |
print("Unifying image with AI...")
|
|
@@ -155,16 +165,14 @@ def process_images(person_img, background_img, num_images, enhance, auto_match,
|
|
| 155 |
enhancer = load_enhancement_model('pretrained/enhancer.pth')
|
| 156 |
combined_img = enhance_unified_image(combined_img, enhancer)
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
return outputs
|
| 161 |
|
| 162 |
-
def gradio_interface(person_img, background_img,
|
| 163 |
try:
|
| 164 |
-
|
| 165 |
-
return
|
| 166 |
except Exception as e:
|
| 167 |
-
return
|
| 168 |
|
| 169 |
def update_enhancement_controls(auto_match):
|
| 170 |
# Disable enhancement sliders if auto-match is checked
|
|
@@ -175,11 +183,10 @@ def update_enhancement_controls(auto_match):
|
|
| 175 |
}
|
| 176 |
|
| 177 |
# Create Gradio interface
|
| 178 |
-
with gr.Blocks() as interface:
|
| 179 |
with gr.Row():
|
| 180 |
person_img = gr.Image(type="pil", label="Upload Person Image")
|
| 181 |
background_img = gr.Image(type="pil", label="Upload Background Image")
|
| 182 |
-
num_images = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Number of Output Images")
|
| 183 |
enhance = gr.Checkbox(label="Enhance Image", value=False)
|
| 184 |
auto_match = gr.Checkbox(label="Auto-Match Enhancers", value=False)
|
| 185 |
contrast_slider = gr.Slider(minimum=0.5, maximum=3.0, step=0.1, value=1.0, label="Contrast")
|
|
@@ -189,14 +196,17 @@ with gr.Blocks() as interface:
|
|
| 189 |
auto_match.change(fn=update_enhancement_controls, inputs=auto_match, outputs=[contrast_slider, brightness_slider, color_slider])
|
| 190 |
|
| 191 |
unify = gr.Checkbox(label="Unify Image with AI", value=True)
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
run_button = gr.Button("Run")
|
| 195 |
|
| 196 |
run_button.click(
|
| 197 |
fn=gradio_interface,
|
| 198 |
-
inputs=[person_img, background_img,
|
| 199 |
-
outputs=
|
| 200 |
)
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
|
|
|
| 87 |
|
| 88 |
return enhanced_img
|
| 89 |
|
| 90 |
+
def embed_person_on_background(person_img, background_img, position_x, position_y, scale):
|
| 91 |
+
# Scale the person image while keeping proportions
|
| 92 |
+
person_width, person_height = person_img.size
|
| 93 |
+
new_width = int(person_width * scale)
|
| 94 |
+
new_height = int(person_height * scale)
|
| 95 |
+
person_img = person_img.resize((new_width, new_height), Image.LANCZOS)
|
| 96 |
+
|
| 97 |
+
# Calculate the position based on bottom-center transformation point
|
| 98 |
+
background_width, background_height = background_img.size
|
| 99 |
+
|
| 100 |
+
# Default position: bottom-center of the background
|
| 101 |
+
default_x = (background_width - new_width) // 2
|
| 102 |
+
default_y = background_height - new_height
|
| 103 |
+
|
| 104 |
+
# Adjust the position based on sliders
|
| 105 |
+
position_x = default_x + int(position_x)
|
| 106 |
+
position_y = default_y + int(position_y)
|
| 107 |
|
| 108 |
# Create a new image with the same size as the background and paste the person image onto it
|
| 109 |
combined_img = Image.new("RGBA", background_img.size)
|
| 110 |
combined_img.paste(background_img, (0, 0))
|
| 111 |
+
combined_img.paste(person_img, (position_x, position_y), person_img)
|
| 112 |
|
| 113 |
return combined_img
|
| 114 |
|
|
|
|
| 145 |
enhanced_image = enhancer.enhance(factor)
|
| 146 |
return enhanced_image
|
| 147 |
|
| 148 |
+
def process_images(person_img, background_img, enhance, auto_match, contrast, brightness, color, unify, position_x, position_y, scale):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
# Remove background from the person image
|
| 150 |
person_no_bg = remove(person_img)
|
| 151 |
|
|
|
|
| 156 |
print(f"Applying enhancement with contrast={contrast}, brightness={brightness}, color={color}...")
|
| 157 |
person_no_bg = enhance_image(person_no_bg, contrast, brightness, color)
|
| 158 |
|
| 159 |
+
combined_img = embed_person_on_background(person_no_bg, background_img, position_x, position_y, scale)
|
| 160 |
|
| 161 |
if unify:
|
| 162 |
print("Unifying image with AI...")
|
|
|
|
| 165 |
enhancer = load_enhancement_model('pretrained/enhancer.pth')
|
| 166 |
combined_img = enhance_unified_image(combined_img, enhancer)
|
| 167 |
|
| 168 |
+
return combined_img
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
def gradio_interface(person_img, background_img, enhance, auto_match, contrast, brightness, color, unify, position_x, position_y, scale):
|
| 171 |
try:
|
| 172 |
+
result = process_images(person_img, background_img, enhance, auto_match, contrast, brightness, color, unify, position_x, position_y, scale)
|
| 173 |
+
return result
|
| 174 |
except Exception as e:
|
| 175 |
+
return str(e)
|
| 176 |
|
| 177 |
def update_enhancement_controls(auto_match):
|
| 178 |
# Disable enhancement sliders if auto-match is checked
|
|
|
|
| 183 |
}
|
| 184 |
|
| 185 |
# Create Gradio interface
|
| 186 |
+
with gr.Blocks(css='#output_image {max-width: 800px !important; width: auto !important; height: auto !important;}') as interface:
|
| 187 |
with gr.Row():
|
| 188 |
person_img = gr.Image(type="pil", label="Upload Person Image")
|
| 189 |
background_img = gr.Image(type="pil", label="Upload Background Image")
|
|
|
|
| 190 |
enhance = gr.Checkbox(label="Enhance Image", value=False)
|
| 191 |
auto_match = gr.Checkbox(label="Auto-Match Enhancers", value=False)
|
| 192 |
contrast_slider = gr.Slider(minimum=0.5, maximum=3.0, step=0.1, value=1.0, label="Contrast")
|
|
|
|
| 196 |
auto_match.change(fn=update_enhancement_controls, inputs=auto_match, outputs=[contrast_slider, brightness_slider, color_slider])
|
| 197 |
|
| 198 |
unify = gr.Checkbox(label="Unify Image with AI", value=True)
|
| 199 |
+
position_x = gr.Slider(minimum=-500, maximum=500, step=1, value=0, label="Horizontal Position (pixels)")
|
| 200 |
+
position_y = gr.Slider(minimum=-500, maximum=500, step=1, value=0, label="Vertical Position (pixels)")
|
| 201 |
+
scale = gr.Slider(minimum=0.1, maximum=3.0, step=0.1, value=1.0, label="Scale")
|
| 202 |
|
| 203 |
+
output = gr.Image(type="pil", label="Generated Image", elem_id="output_image")
|
| 204 |
run_button = gr.Button("Run")
|
| 205 |
|
| 206 |
run_button.click(
|
| 207 |
fn=gradio_interface,
|
| 208 |
+
inputs=[person_img, background_img, enhance, auto_match, contrast_slider, brightness_slider, color_slider, unify, position_x, position_y, scale],
|
| 209 |
+
outputs=output
|
| 210 |
)
|
| 211 |
|
| 212 |
if __name__ == "__main__":
|