README, config file

This commit is contained in:
2026-07-23 23:48:05 -07:00
parent 504794f9a8
commit 05e70a7fc4
7 changed files with 150 additions and 36 deletions
+11 -6
View File
@@ -6,24 +6,29 @@ import datetime
from pathlib import Path
from typing import Any, Dict, List, Tuple
from PIL import Image, ImageOps
import config
from tools.utils import format_relative_time, ToolError, get_file_info_list, sort_file_list, parse_indices
logger = logging.getLogger("MattCP")
def calculate_patch_dimensions(orig_w: int, orig_h: int, target_tokens: int = 70):
def calculate_patch_dimensions(orig_w: int, orig_h: int, target_tokens: int = None):
"""
Calculates optimal pixel dimensions to hit a token budget of at least target_tokens.
Validates budget based on actual rounded pixel dimensions to avoid float precision errors.
"""
if target_tokens is None:
target_tokens = config.PREVIEW_TOKEN_BUDGET
ar = orig_w / orig_h
# Case A: Width is the anchor (non-padded)
w_anchor = 1
while True:
w_px = w_anchor * 48
w_px = w_anchor * config.PATCH_SIZE
h_px = round(w_px / ar)
# Calculate actual tokens based on resulting pixel dimensions
tokens = math.ceil(w_px / 48) * math.ceil(h_px / 48)
tokens = math.ceil(w_px / config.PATCH_SIZE) * math.ceil(h_px / config.PATCH_SIZE)
if tokens >= target_tokens:
res_a = {"w": w_px, "h": h_px, "tokens": tokens}
break
@@ -32,10 +37,10 @@ def calculate_patch_dimensions(orig_w: int, orig_h: int, target_tokens: int = 70
# Case B: Height is the anchor (non-padded)
h_anchor = 1
while True:
h_px = h_anchor * 48
h_px = h_anchor * config.PATCH_SIZE
w_px = round(h_px * ar)
# Calculate actual tokens based on resulting pixel dimensions
tokens = math.ceil(w_px / 48) * math.ceil(h_px / 48)
tokens = math.ceil(w_px / config.PATCH_SIZE) * math.ceil(h_px / config.PATCH_SIZE)
if tokens >= target_tokens:
res_b = {"w": w_px, "h": h_px, "tokens": tokens}
break
@@ -100,7 +105,7 @@ async def handle(args: Dict[str, Any]):
thumb.thumbnail((target_w, target_h), Image.Resampling.LANCZOS)
buf = io.BytesIO()
thumb.save(buf, format='JPEG', quality=95)
thumb.save(buf, format='JPEG', quality=config.IMAGE_QUALITY)
img_data = base64.b64encode(buf.getvalue()).decode("utf-8")
# Gather info