Contact sheet small size fixes

This commit is contained in:
2026-07-26 22:17:34 -07:00
parent f676a07a71
commit e9ee89f8a3
+24 -11
View File
@@ -2,7 +2,9 @@ import base64
import logging
import io
import datetime
import math
from pathlib import Path
from typing import Any, Dict, List
from PIL import Image, ImageDraw, ImageFont, ImageOps
import config
@@ -14,8 +16,7 @@ logger = logging.getLogger("MooseCP")
async def handle(args: Dict[str, Any]):
"""
Generates a contact sheet of images.
Grid: 10 columns x 7 rows (70 images total).
Sized for optimal token usage (1120 tokens) on llama.cpp.
Sized for optimal token usage according to config values.
"""
dir_path_str = args.get("path")
page = int(args.get("page", 1))
@@ -44,10 +45,10 @@ async def handle(args: Dict[str, Any]):
}
sort_label = sort_labels.get(sort_by, "Name (alphabetical)")
# Fixed grid dimensions for token optimization
COLS = config.CONTACT_SHEET_COLS
ROWS = config.CONTACT_SHEET_ROWS
PAGE_SIZE = COLS * ROWS
# Grid dimensions for token optimization (see config.py)
MAX_COLS = config.CONTACT_SHEET_COLS
MAX_ROWS = config.CONTACT_SHEET_ROWS
PAGE_SIZE = MAX_COLS * MAX_ROWS
total_files = len(file_info_list)
@@ -56,9 +57,21 @@ async def handle(args: Dict[str, Any]):
if not paged_files:
return {"text": f"No images found on page {page}."}
thumb_size = 192 # 192 / 48 = 4 patches per side
canvas_w = COLS * thumb_size
canvas_h = ROWS * thumb_size
# Dynamic grid sizing to avoid blank space
n_images = len(paged_files)
thumb_size = config.CONTACT_SHEET_THUMB_SIZE
if n_images <= config.CONTACT_SHEET_ROWS ** 2:
# Smallest square-ish grid that fits all
cols = math.ceil(math.sqrt(n_images))
rows = math.ceil(n_images / cols)
else:
# Lock rows, expand columns
rows = config.CONTACT_SHEET_ROWS
cols = math.ceil(n_images / config.CONTACT_SHEET_ROWS)
canvas_w = cols * thumb_size
canvas_h = rows * thumb_size
canvas = Image.new('RGB', (canvas_w, canvas_h), (30, 30, 30))
draw = ImageDraw.Draw(canvas)
@@ -87,8 +100,8 @@ async def handle(args: Dict[str, Any]):
start_idx = (page - 1) * PAGE_SIZE
for i, info in enumerate(paged_files):
full_path = info["path"]
row = i // COLS
col = i % COLS
row = i // cols
col = i % cols
x = col * thumb_size
y = row * thumb_size