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 logging
import io import io
import datetime import datetime
import math
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List from typing import Any, Dict, List
from PIL import Image, ImageDraw, ImageFont, ImageOps from PIL import Image, ImageDraw, ImageFont, ImageOps
import config import config
@@ -14,8 +16,7 @@ logger = logging.getLogger("MooseCP")
async def handle(args: Dict[str, Any]): async def handle(args: Dict[str, Any]):
""" """
Generates a contact sheet of images. Generates a contact sheet of images.
Grid: 10 columns x 7 rows (70 images total). Sized for optimal token usage according to config values.
Sized for optimal token usage (1120 tokens) on llama.cpp.
""" """
dir_path_str = args.get("path") dir_path_str = args.get("path")
page = int(args.get("page", 1)) 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)") sort_label = sort_labels.get(sort_by, "Name (alphabetical)")
# Fixed grid dimensions for token optimization # Grid dimensions for token optimization (see config.py)
COLS = config.CONTACT_SHEET_COLS MAX_COLS = config.CONTACT_SHEET_COLS
ROWS = config.CONTACT_SHEET_ROWS MAX_ROWS = config.CONTACT_SHEET_ROWS
PAGE_SIZE = COLS * ROWS PAGE_SIZE = MAX_COLS * MAX_ROWS
total_files = len(file_info_list) total_files = len(file_info_list)
@@ -56,9 +57,21 @@ async def handle(args: Dict[str, Any]):
if not paged_files: if not paged_files:
return {"text": f"No images found on page {page}."} return {"text": f"No images found on page {page}."}
thumb_size = 192 # 192 / 48 = 4 patches per side # Dynamic grid sizing to avoid blank space
canvas_w = COLS * thumb_size n_images = len(paged_files)
canvas_h = ROWS * thumb_size 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)) canvas = Image.new('RGB', (canvas_w, canvas_h), (30, 30, 30))
draw = ImageDraw.Draw(canvas) draw = ImageDraw.Draw(canvas)
@@ -87,8 +100,8 @@ async def handle(args: Dict[str, Any]):
start_idx = (page - 1) * PAGE_SIZE start_idx = (page - 1) * PAGE_SIZE
for i, info in enumerate(paged_files): for i, info in enumerate(paged_files):
full_path = info["path"] full_path = info["path"]
row = i // COLS row = i // cols
col = i % COLS col = i % cols
x = col * thumb_size x = col * thumb_size
y = row * thumb_size y = row * thumb_size