Spaces:
Sleeping
Sleeping
add watermark and output selection
Browse files
app.py
CHANGED
@@ -49,7 +49,7 @@ def remove_background(input_path):
|
|
49 |
img = Image.open(io.BytesIO(output_image)).convert("RGBA")
|
50 |
return img
|
51 |
|
52 |
-
def process_single_image(image_path, output_folder, crop_mode, remove_bg):
|
53 |
filename = os.path.basename(image_path)
|
54 |
try:
|
55 |
if remove_bg == 'yes':
|
@@ -63,9 +63,36 @@ def process_single_image(image_path, output_folder, crop_mode, remove_bg):
|
|
63 |
# Resize and crop the image with or without background removal
|
64 |
new_image = resize_and_crop_image(temp_image_path, crop_mode=crop_mode)
|
65 |
|
|
|
|
|
|
|
|
|
|
|
66 |
# Save the final image
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
if remove_bg == 'yes':
|
71 |
# Remove the temporary file
|
@@ -77,7 +104,7 @@ def process_single_image(image_path, output_folder, crop_mode, remove_bg):
|
|
77 |
print(f"Error processing {filename}: {e}")
|
78 |
return None
|
79 |
|
80 |
-
def process_images(zip_file, crop_mode='center', remove_bg='yes', progress=gr.Progress()):
|
81 |
# Create a temporary directory
|
82 |
input_folder = "temp_input"
|
83 |
output_folder = "temp_output"
|
@@ -98,7 +125,7 @@ def process_images(zip_file, crop_mode='center', remove_bg='yes', progress=gr.Pr
|
|
98 |
|
99 |
# Process images using ThreadPoolExecutor
|
100 |
with ThreadPoolExecutor(max_workers=2) as executor:
|
101 |
-
future_to_image = {executor.submit(process_single_image, image_path, output_folder, crop_mode, remove_bg): image_path for image_path in image_files}
|
102 |
for idx, future in enumerate(future_to_image):
|
103 |
result = future.result()
|
104 |
if result:
|
@@ -115,29 +142,43 @@ def process_images(zip_file, crop_mode='center', remove_bg='yes', progress=gr.Pr
|
|
115 |
# Return the images and the zip file path
|
116 |
return processed_images, output_zip_path
|
117 |
|
118 |
-
def gradio_interface(zip_file, crop_mode, remove_bg):
|
119 |
progress = gr.Progress() # Initialize progress
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
# Create the Gradio interface
|
123 |
with gr.Blocks() as iface:
|
124 |
-
gr.Markdown("# Image Background Removal and Resizing")
|
125 |
-
gr.Markdown("Upload a ZIP or RAR file containing images, choose the crop mode, and
|
126 |
-
|
127 |
with gr.Row():
|
128 |
zip_file = gr.File(label="Upload ZIP/RAR file of images", file_types=[".zip", ".rar"])
|
|
|
|
|
|
|
129 |
crop_mode = gr.Radio(choices=["center", "top", "bottom", "left", "right"], label="Crop Mode", value="center")
|
130 |
remove_bg = gr.Radio(choices=["yes", "no"], label="Remove Background", value="yes")
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
gallery = gr.Gallery(label="Processed Images")
|
133 |
output_zip = gr.File(label="Download Processed Images as ZIP")
|
134 |
|
135 |
-
def process(zip_file, crop_mode, remove_bg):
|
136 |
-
processed_images, zip_path = gradio_interface(zip_file, crop_mode, remove_bg)
|
137 |
return processed_images, zip_path
|
138 |
|
139 |
process_button = gr.Button("Process Images")
|
140 |
-
process_button.click(process, inputs=[zip_file, crop_mode, remove_bg], outputs=[gallery, output_zip])
|
141 |
|
142 |
# Launch the interface
|
143 |
iface.launch()
|
|
|
49 |
img = Image.open(io.BytesIO(output_image)).convert("RGBA")
|
50 |
return img
|
51 |
|
52 |
+
def process_single_image(image_path, output_folder, crop_mode, remove_bg, output_format, bg_choice, watermark_path=None):
|
53 |
filename = os.path.basename(image_path)
|
54 |
try:
|
55 |
if remove_bg == 'yes':
|
|
|
63 |
# Resize and crop the image with or without background removal
|
64 |
new_image = resize_and_crop_image(temp_image_path, crop_mode=crop_mode)
|
65 |
|
66 |
+
# Apply watermark if provided
|
67 |
+
if watermark_path:
|
68 |
+
watermark = Image.open(watermark_path).convert("RGBA")
|
69 |
+
new_image.paste(watermark, (0, 0), watermark)
|
70 |
+
|
71 |
# Save the final image
|
72 |
+
if remove_bg == 'yes' and output_format == 'PNG' and bg_choice == 'transparent':
|
73 |
+
output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.png")
|
74 |
+
new_image.save(output_path, format='PNG')
|
75 |
+
elif remove_bg == 'yes' and output_format == 'PNG' and bg_choice == 'white':
|
76 |
+
# Create a white background
|
77 |
+
background = Image.new("RGBA", new_image.size, (255, 255, 255, 255))
|
78 |
+
# Paste the image with no background onto the white background
|
79 |
+
background.paste(new_image, mask=new_image.split()[3]) # 3 is the alpha channel
|
80 |
+
output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.png")
|
81 |
+
background.convert('RGB').save(output_path, format='PNG')
|
82 |
+
elif remove_bg == 'yes' and output_format == 'JPG':
|
83 |
+
# Create a white background
|
84 |
+
background = Image.new("RGB", new_image.size, (255, 255, 255))
|
85 |
+
# Paste the image with no background onto the white background
|
86 |
+
background.paste(new_image, mask=new_image.split()[3]) # 3 is the alpha channel
|
87 |
+
output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.jpg")
|
88 |
+
background.save(output_path, format='JPEG')
|
89 |
+
else:
|
90 |
+
output_ext = 'jpg' if output_format == 'JPG' else 'png'
|
91 |
+
output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.{output_ext}")
|
92 |
+
if output_format == 'JPG':
|
93 |
+
new_image.convert('RGB').save(output_path, format='JPEG')
|
94 |
+
else:
|
95 |
+
new_image.save(output_path, format='PNG')
|
96 |
|
97 |
if remove_bg == 'yes':
|
98 |
# Remove the temporary file
|
|
|
104 |
print(f"Error processing {filename}: {e}")
|
105 |
return None
|
106 |
|
107 |
+
def process_images(zip_file, crop_mode='center', remove_bg='yes', watermark_path=None, output_format='PNG', bg_choice='transparent', progress=gr.Progress()):
|
108 |
# Create a temporary directory
|
109 |
input_folder = "temp_input"
|
110 |
output_folder = "temp_output"
|
|
|
125 |
|
126 |
# Process images using ThreadPoolExecutor
|
127 |
with ThreadPoolExecutor(max_workers=2) as executor:
|
128 |
+
future_to_image = {executor.submit(process_single_image, image_path, output_folder, crop_mode, remove_bg, output_format, bg_choice, watermark_path): image_path for image_path in image_files}
|
129 |
for idx, future in enumerate(future_to_image):
|
130 |
result = future.result()
|
131 |
if result:
|
|
|
142 |
# Return the images and the zip file path
|
143 |
return processed_images, output_zip_path
|
144 |
|
145 |
+
def gradio_interface(zip_file, crop_mode, remove_bg, watermark, output_format, bg_choice):
|
146 |
progress = gr.Progress() # Initialize progress
|
147 |
+
watermark_path = watermark.name if watermark else None
|
148 |
+
return process_images(zip_file.name, crop_mode, remove_bg, watermark_path, output_format, bg_choice, progress)
|
149 |
+
|
150 |
+
def show_bg_choice(remove_bg, output_format):
|
151 |
+
if remove_bg == 'yes' and output_format == 'PNG':
|
152 |
+
return gr.update(visible=True)
|
153 |
+
return gr.update(visible=False)
|
154 |
|
155 |
# Create the Gradio interface
|
156 |
with gr.Blocks() as iface:
|
157 |
+
gr.Markdown("# Image Background Removal and Resizing with Optional Watermark")
|
158 |
+
gr.Markdown("Upload a ZIP or RAR file containing images, choose the crop mode, optionally upload a watermark image, and select the output format.")
|
159 |
+
|
160 |
with gr.Row():
|
161 |
zip_file = gr.File(label="Upload ZIP/RAR file of images", file_types=[".zip", ".rar"])
|
162 |
+
watermark = gr.File(label="Upload Watermark Image (Optional)", file_types=[".png"])
|
163 |
+
|
164 |
+
with gr.Row():
|
165 |
crop_mode = gr.Radio(choices=["center", "top", "bottom", "left", "right"], label="Crop Mode", value="center")
|
166 |
remove_bg = gr.Radio(choices=["yes", "no"], label="Remove Background", value="yes")
|
167 |
+
output_format = gr.Radio(choices=["PNG", "JPG"], label="Output Format", value="PNG")
|
168 |
+
bg_choice = gr.Radio(choices=["transparent", "white"], label="Background Choice", value="transparent", visible=True)
|
169 |
+
|
170 |
+
remove_bg.change(show_bg_choice, inputs=[remove_bg, output_format], outputs=bg_choice)
|
171 |
+
output_format.change(show_bg_choice, inputs=[remove_bg, output_format], outputs=bg_choice)
|
172 |
|
173 |
gallery = gr.Gallery(label="Processed Images")
|
174 |
output_zip = gr.File(label="Download Processed Images as ZIP")
|
175 |
|
176 |
+
def process(zip_file, crop_mode, remove_bg, watermark, output_format, bg_choice):
|
177 |
+
processed_images, zip_path = gradio_interface(zip_file, crop_mode, remove_bg, watermark, output_format, bg_choice)
|
178 |
return processed_images, zip_path
|
179 |
|
180 |
process_button = gr.Button("Process Images")
|
181 |
+
process_button.click(process, inputs=[zip_file, crop_mode, remove_bg, watermark, output_format, bg_choice], outputs=[gallery, output_zip])
|
182 |
|
183 |
# Launch the interface
|
184 |
iface.launch()
|