update revisi
Browse files
app.py
CHANGED
@@ -458,37 +458,40 @@ 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
|
463 |
if aspect_ratio > 1: # landscape
|
464 |
-
new_width = target_width
|
465 |
new_height = int(new_width / aspect_ratio)
|
466 |
-
if new_height >
|
467 |
-
new_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 >
|
473 |
-
new_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 |
|
482 |
-
|
483 |
-
|
484 |
-
y = (target_height - new_height) // 2 + top_offset
|
485 |
-
|
486 |
-
# Pastikan gambar tidak keluar dari canvas di bawah
|
487 |
-
if y + new_height > target_height:
|
488 |
-
y = target_height - new_height
|
489 |
-
|
490 |
-
print(f"Image centered with top offset: size={new_width}x{new_height}, position=({x},{y})")
|
491 |
-
log = [{"action": "resize_and_center", "new_size": f"{new_width}x{new_height}", "position": f"{x},{y}"}]
|
492 |
return log, image, x, y
|
493 |
|
494 |
# ------------------ Qwen 2.5VL Inference Functions & Model Loading ------------------
|
|
|
458 |
target_width, target_height = canvas_size
|
459 |
aspect_ratio = image.width / image.height
|
460 |
|
461 |
+
# Berikan margin di semua sisi (misalnya 50px dari setiap tepi)
|
462 |
+
margin = 50
|
463 |
+
available_width = target_width - (2 * margin)
|
464 |
+
available_height = target_height - (2 * margin)
|
465 |
+
|
466 |
+
# Scale factor untuk memperkecil gambar (85% dari ukuran available space)
|
467 |
+
scale_factor = 0.85
|
468 |
+
max_width = int(available_width * scale_factor)
|
469 |
+
max_height = int(available_height * scale_factor)
|
470 |
+
|
471 |
# Tentukan ukuran yang tepat dengan mempertahankan aspect ratio
|
472 |
+
# dan memastikan gambar tidak terlalu besar (diperkecil dulu)
|
473 |
if aspect_ratio > 1: # landscape
|
474 |
+
new_width = min(max_width, target_width - (2 * margin))
|
475 |
new_height = int(new_width / aspect_ratio)
|
476 |
+
if new_height > max_height:
|
477 |
+
new_height = max_height
|
478 |
new_width = int(new_height * aspect_ratio)
|
479 |
else: # portrait
|
480 |
+
new_height = min(max_height, target_height - (2 * margin))
|
481 |
new_width = int(new_height * aspect_ratio)
|
482 |
+
if new_width > max_width:
|
483 |
+
new_width = max_width
|
484 |
new_height = int(new_width / aspect_ratio)
|
485 |
|
486 |
+
# Resize gambar dengan ukuran baru (lebih kecil)
|
487 |
image = image.resize((new_width, new_height), Image.LANCZOS)
|
488 |
|
489 |
+
# Posisi tengah canvas
|
490 |
x = (target_width - new_width) // 2
|
491 |
+
y = (target_height - new_height) // 2
|
492 |
|
493 |
+
print(f"Image scaled down and centered: original_size={image.size}, new_size={new_width}x{new_height}, position=({x},{y}), margin={margin}px")
|
494 |
+
log = [{"action": "scale_down_and_center", "new_size": f"{new_width}x{new_height}", "position": f"{x},{y}", "margin": f"{margin}px"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
495 |
return log, image, x, y
|
496 |
|
497 |
# ------------------ Qwen 2.5VL Inference Functions & Model Loading ------------------
|