Spaces:
Sleeping
Sleeping
add thread
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from PIL import Image
|
|
5 |
import io
|
6 |
from rembg import remove
|
7 |
import gradio as gr
|
|
|
8 |
|
9 |
def resize_and_crop_image(image_path, target_size=(1080, 1080), crop_mode='center'):
|
10 |
with Image.open(image_path) as img:
|
@@ -48,6 +49,34 @@ def remove_background(input_path):
|
|
48 |
img = Image.open(io.BytesIO(output_image)).convert("RGBA")
|
49 |
return img
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def process_images(zip_file, crop_mode='center', remove_bg='yes', progress=gr.Progress()):
|
52 |
# Create a temporary directory
|
53 |
input_folder = "temp_input"
|
@@ -64,37 +93,18 @@ def process_images(zip_file, crop_mode='center', remove_bg='yes', progress=gr.Pr
|
|
64 |
zip_ref.extractall(input_folder)
|
65 |
|
66 |
processed_images = []
|
67 |
-
image_files = [f for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
|
68 |
total_images = len(image_files)
|
69 |
-
for idx, filename in enumerate(image_files):
|
70 |
-
image_path = os.path.join(input_folder, filename)
|
71 |
-
try:
|
72 |
-
if remove_bg == 'yes':
|
73 |
-
# Remove background
|
74 |
-
image_with_no_bg = remove_background(image_path)
|
75 |
-
temp_image_path = os.path.join(output_folder, f"temp_{filename}")
|
76 |
-
image_with_no_bg.save(temp_image_path, format='PNG')
|
77 |
-
else:
|
78 |
-
temp_image_path = image_path
|
79 |
-
|
80 |
-
# Resize and crop the image with or without background removal
|
81 |
-
new_image = resize_and_crop_image(temp_image_path, crop_mode=crop_mode)
|
82 |
-
|
83 |
-
# Save the final image
|
84 |
-
output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.png")
|
85 |
-
new_image.save(output_path, format='PNG')
|
86 |
-
|
87 |
-
processed_images.append(output_path)
|
88 |
-
|
89 |
-
if remove_bg == 'yes':
|
90 |
-
# Remove the temporary file
|
91 |
-
os.remove(temp_image_path)
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
# Update progress
|
94 |
progress((idx + 1) / total_images, f"{idx + 1}/{total_images} images processed")
|
95 |
-
|
96 |
-
except Exception as e:
|
97 |
-
print(f"Error processing {filename}: {e}")
|
98 |
|
99 |
# Create a zip file of the processed images
|
100 |
output_zip_path = "processed_images.zip"
|
|
|
5 |
import io
|
6 |
from rembg import remove
|
7 |
import gradio as gr
|
8 |
+
from concurrent.futures import ThreadPoolExecutor
|
9 |
|
10 |
def resize_and_crop_image(image_path, target_size=(1080, 1080), crop_mode='center'):
|
11 |
with Image.open(image_path) as img:
|
|
|
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':
|
56 |
+
# Remove background
|
57 |
+
image_with_no_bg = remove_background(image_path)
|
58 |
+
temp_image_path = os.path.join(output_folder, f"temp_{filename}")
|
59 |
+
image_with_no_bg.save(temp_image_path, format='PNG')
|
60 |
+
else:
|
61 |
+
temp_image_path = image_path
|
62 |
+
|
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 |
+
output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.png")
|
68 |
+
new_image.save(output_path, format='PNG')
|
69 |
+
|
70 |
+
if remove_bg == 'yes':
|
71 |
+
# Remove the temporary file
|
72 |
+
os.remove(temp_image_path)
|
73 |
+
|
74 |
+
return output_path
|
75 |
+
|
76 |
+
except Exception as e:
|
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"
|
|
|
93 |
zip_ref.extractall(input_folder)
|
94 |
|
95 |
processed_images = []
|
96 |
+
image_files = [os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
|
97 |
total_images = len(image_files)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
105 |
+
processed_images.append(result)
|
106 |
# Update progress
|
107 |
progress((idx + 1) / total_images, f"{idx + 1}/{total_images} images processed")
|
|
|
|
|
|
|
108 |
|
109 |
# Create a zip file of the processed images
|
110 |
output_zip_path = "processed_images.zip"
|