# -*- coding: utf-8 -*-
"""Lô 09 — PHIẾU LÀM MẪU MÀU (ảnh phiếu thật): lưu ảnh + cắt ô zoom dạng lưới để AGY đọc chữ viết tay."""
import os, shutil
from PIL import Image

D = r'C:\Claw 2\Nghien cuu\Du-an-A-Quy-trinh-son'
SRC = os.path.join(os.environ['LOCALAPPDATA'], 'hermes', 'cache', 'images', 'img_5b9f6529808f.jpg')
OUT = os.path.join(D, 'phieu_mau', 'PT-008H')
os.makedirs(OUT, exist_ok=True)

im = Image.open(SRC).convert('RGB')
W, H = im.size
print('ảnh phiếu:', os.path.getsize(SRC), 'byte | %dx%d px' % (W, H))
shutil.copy2(SRC, os.path.join(OUT, 'PT-008H_toan-bo.jpg'))

# lưới 3 cột × 4 hàng, chồng lề 15% — phóng để đọc chữ viết tay
cols, rows, ov = 3, 4, 0.15
cw, ch = int(W / cols), int(H / rows)
n = 0
for r in range(rows):
    for c in range(cols):
        x0 = max(0, int(c * cw - cw * ov))
        y0 = max(0, int(r * ch - ch * ov))
        x1 = min(W, int((c + 1) * cw + cw * ov))
        y1 = min(H, int((r + 1) * ch + ch * ov))
        crop = im.crop((x0, y0, x1, y1))
        w, h = crop.size
        scale = max(1.0, 1400.0 / max(w, 1))
        if scale > 1:
            crop = crop.resize((int(w * scale), int(h * scale)), Image.LANCZOS)
        n += 1
        p = os.path.join(OUT, 'phieu_o%02d_%dx%d.jpg' % (n, r + 1, c + 1))
        crop.save(p, quality=95)
print('đã cắt', n, 'ô (lưới %dx%d, phóng tối đa ~1400 px/cạnh)' % (rows, cols))
print('thư mục:', OUT)
for f in sorted(os.listdir(OUT)):
    print('   ', f, os.path.getsize(os.path.join(OUT, f)), 'byte')
