updateed center
Browse files
app.py
CHANGED
@@ -457,15 +457,30 @@ def position_logic_old(image_path, canvas_size, padding_top, padding_right, padd
|
|
457 |
def position_logic_none(image, canvas_size):
|
458 |
target_width, target_height = canvas_size
|
459 |
aspect_ratio = image.width / image.height
|
460 |
-
|
|
|
|
|
|
|
461 |
new_width = target_width
|
462 |
new_height = int(new_width / aspect_ratio)
|
463 |
-
|
|
|
|
|
|
|
464 |
new_height = target_height
|
465 |
new_width = int(new_height * aspect_ratio)
|
|
|
|
|
|
|
|
|
|
|
466 |
image = image.resize((new_width, new_height), Image.LANCZOS)
|
|
|
|
|
467 |
x = (target_width - new_width) // 2
|
468 |
y = (target_height - new_height) // 2
|
|
|
|
|
469 |
log = [{"action": "resize_and_center", "new_size": f"{new_width}x{new_height}", "position": f"{x},{y}"}]
|
470 |
return log, image, x, y
|
471 |
|
@@ -1124,13 +1139,8 @@ def process_single_image(
|
|
1124 |
temp_image_path = os.path.join(output_folder, f"temp_{filename}")
|
1125 |
image_with_no_bg.save(temp_image_path, format='PNG')
|
1126 |
|
1127 |
-
#
|
1128 |
-
|
1129 |
-
effective_padding_bottom = 0 if snap_to_bottom else padding_bottom
|
1130 |
-
effective_padding_left = 0 if snap_to_left else padding_left
|
1131 |
-
effective_padding_right = 0 if snap_to_right else padding_right
|
1132 |
-
|
1133 |
-
# Cetak status snap untuk debugging
|
1134 |
if snap_to_left:
|
1135 |
print(f"Snap to Left active: Forcing padding_left = 0 (original: {padding_left})")
|
1136 |
if snap_to_right:
|
@@ -1139,16 +1149,10 @@ def process_single_image(
|
|
1139 |
print(f"Snap to Top active: Forcing padding_top = 0 (original: {padding_top})")
|
1140 |
if snap_to_bottom:
|
1141 |
print(f"Snap to Bottom active: Forcing padding_bottom = 0 (original: {padding_bottom})")
|
1142 |
-
|
1143 |
-
|
1144 |
-
|
1145 |
-
|
1146 |
-
effective_padding_right,
|
1147 |
-
effective_padding_bottom,
|
1148 |
-
effective_padding_left,
|
1149 |
-
use_threshold=True, bg_method=bg_method, is_person=snap_to_bottom,
|
1150 |
-
snap_to_top=snap_to_top, snap_to_left=snap_to_left, snap_to_right=snap_to_right
|
1151 |
-
)
|
1152 |
if bg_choice == 'white':
|
1153 |
canvas = Image.new("RGBA", canvas_size, "WHITE")
|
1154 |
elif bg_choice == 'custom':
|
@@ -1180,24 +1184,13 @@ def process_single_image(
|
|
1180 |
new_canvas = Image.new("RGBA", canvas_size, dom_col)
|
1181 |
else:
|
1182 |
new_canvas = Image.new("RGBA", canvas_size, (0, 0, 0, 0))
|
1183 |
-
|
1184 |
-
|
1185 |
-
|
1186 |
-
|
1187 |
-
|
1188 |
-
if new_width_h > available_width:
|
1189 |
-
scale_factor = available_width / rs_w
|
1190 |
-
new_width = available_width
|
1191 |
-
new_height = int(rs_h * scale_factor)
|
1192 |
-
else:
|
1193 |
-
new_width = new_width_h
|
1194 |
-
new_height = target_height
|
1195 |
-
rotated_subject = rotated_subject.resize((new_width, new_height), Image.LANCZOS)
|
1196 |
-
new_x = padding_left + (available_width - new_width) // 2
|
1197 |
-
new_y = padding_top + (target_height - new_height) // 2
|
1198 |
-
new_canvas.paste(rotated_subject, (new_x, new_y), rotated_subject)
|
1199 |
canvas = new_canvas
|
1200 |
-
logs.append({"action": "
|
1201 |
out_ext = "jpg" if output_format == "JPG" else "png"
|
1202 |
out_filename = f"{os.path.splitext(filename)[0]}.{out_ext}"
|
1203 |
out_path = os.path.join(output_folder, out_filename)
|
@@ -1667,7 +1660,7 @@ def preset_snap_rules(filename, image_path=None):
|
|
1667 |
with gr.Blocks(theme='allenai/gradio-theme') as iface:
|
1668 |
gr.Markdown("## Image BG Removal with Rotation, Watermark, Twibbon & Classifications for Padding Override")
|
1669 |
with gr.Row():
|
1670 |
-
input_files = gr.File(label="Upload (Image(s)/ZIP)", file_types=[".zip", ".rar", "image"], interactive=True)
|
1671 |
watermark = gr.File(label="Watermark (Optional)", file_types=[".png"])
|
1672 |
twibbon = gr.File(label="Twibbon (Optional)", file_types=[".png"])
|
1673 |
sheet_file = gr.File(label="Upload Sheet (.xlsx/.csv)", file_types=[".xlsx", ".csv"], interactive=True)
|
|
|
457 |
def position_logic_none(image, canvas_size):
|
458 |
target_width, target_height = canvas_size
|
459 |
aspect_ratio = image.width / image.height
|
460 |
+
|
461 |
+
# Tentukan ukuran yang tepat dengan mempertahankan aspect ratio
|
462 |
+
# dan memastikan gambar muat dalam canvas
|
463 |
+
if aspect_ratio > 1: # landscape
|
464 |
new_width = target_width
|
465 |
new_height = int(new_width / aspect_ratio)
|
466 |
+
if new_height > target_height:
|
467 |
+
new_height = target_height
|
468 |
+
new_width = int(new_height * aspect_ratio)
|
469 |
+
else: # portrait
|
470 |
new_height = target_height
|
471 |
new_width = int(new_height * aspect_ratio)
|
472 |
+
if new_width > target_width:
|
473 |
+
new_width = target_width
|
474 |
+
new_height = int(new_width / aspect_ratio)
|
475 |
+
|
476 |
+
# Resize gambar dengan ukuran baru
|
477 |
image = image.resize((new_width, new_height), Image.LANCZOS)
|
478 |
+
|
479 |
+
# Posisi tengah canvas
|
480 |
x = (target_width - new_width) // 2
|
481 |
y = (target_height - new_height) // 2
|
482 |
+
|
483 |
+
print(f"Image centered: size={new_width}x{new_height}, position=({x},{y})")
|
484 |
log = [{"action": "resize_and_center", "new_size": f"{new_width}x{new_height}", "position": f"{x},{y}"}]
|
485 |
return log, image, x, y
|
486 |
|
|
|
1139 |
temp_image_path = os.path.join(output_folder, f"temp_{filename}")
|
1140 |
image_with_no_bg.save(temp_image_path, format='PNG')
|
1141 |
|
1142 |
+
# Selalu gunakan position_logic_none untuk centering gambar
|
1143 |
+
# Kode snap masih disimpan untuk kompatibilitas
|
|
|
|
|
|
|
|
|
|
|
1144 |
if snap_to_left:
|
1145 |
print(f"Snap to Left active: Forcing padding_left = 0 (original: {padding_left})")
|
1146 |
if snap_to_right:
|
|
|
1149 |
print(f"Snap to Top active: Forcing padding_top = 0 (original: {padding_top})")
|
1150 |
if snap_to_bottom:
|
1151 |
print(f"Snap to Bottom active: Forcing padding_bottom = 0 (original: {padding_bottom})")
|
1152 |
+
|
1153 |
+
# Gunakan position_logic_none untuk memastikan semua gambar diletakkan di tengah
|
1154 |
+
image = Image.open(temp_image_path)
|
1155 |
+
logs, cropped_img, x, y = position_logic_none(image, canvas_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
1156 |
if bg_choice == 'white':
|
1157 |
canvas = Image.new("RGBA", canvas_size, "WHITE")
|
1158 |
elif bg_choice == 'custom':
|
|
|
1184 |
new_canvas = Image.new("RGBA", canvas_size, dom_col)
|
1185 |
else:
|
1186 |
new_canvas = Image.new("RGBA", canvas_size, (0, 0, 0, 0))
|
1187 |
+
|
1188 |
+
# Gunakan position_logic_none untuk rotated image juga
|
1189 |
+
_, rotated_sized_img, rotated_x, rotated_y = position_logic_none(rotated_subject, canvas_size)
|
1190 |
+
|
1191 |
+
new_canvas.paste(rotated_sized_img, (rotated_x, rotated_y), rotated_sized_img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1192 |
canvas = new_canvas
|
1193 |
+
logs.append({"action": "rotate_final_centered", "rotation": rotation, "direction": direction})
|
1194 |
out_ext = "jpg" if output_format == "JPG" else "png"
|
1195 |
out_filename = f"{os.path.splitext(filename)[0]}.{out_ext}"
|
1196 |
out_path = os.path.join(output_folder, out_filename)
|
|
|
1660 |
with gr.Blocks(theme='allenai/gradio-theme') as iface:
|
1661 |
gr.Markdown("## Image BG Removal with Rotation, Watermark, Twibbon & Classifications for Padding Override")
|
1662 |
with gr.Row():
|
1663 |
+
input_files = gr.File(label="Upload (Image(s)/ZIP/RAR)", file_types=[".zip", ".rar", "image"], interactive=True)
|
1664 |
watermark = gr.File(label="Watermark (Optional)", file_types=[".png"])
|
1665 |
twibbon = gr.File(label="Twibbon (Optional)", file_types=[".png"])
|
1666 |
sheet_file = gr.File(label="Upload Sheet (.xlsx/.csv)", file_types=[".xlsx", ".csv"], interactive=True)
|