"""A free proof is a 15 cm window around the measured fine stroke, not the top, and samples of the colours that window lacks, up to 30 cm (Marcus, 2026-11-26).""" import io import tempfile import unittest from pathlib import Path from PIL import Image, ImageDraw from app.printpng import render PX_CM = 300 / 2.54 def item(height_cm=30, fraction='0.75'): return { 'mode': 'file', 'metres': '10.5', 'billed_metres': '0', 'production': { 'version': 3, 'film_width_cm': 20, 'height_cm': height_cm, 'sources': [{'upload_id': '00000000-0000-0000-0000-000000000001', 'kind': 'sheet', 'width_cm': 20, 'length_cm': height_cm, 'copies': 1, 'rotation_degrees': 0, 'mirrored': False, 'measurement': 'file'}], 'placements': [{'source_index': 0, 'copy_index': 0, 'x_cm': 0, 'y_cm': 0, 'width_cm': 20, 'length_cm': height_cm, 'rotation_degrees': 0, 'mirrored': False}], }, 'proof_parent_item_index': 0, 'proof_y_fraction': fraction, } RED, BLUE, GREEN = (220, 30, 60, 255), (30, 60, 220, 255), (40, 180, 70, 255) def cm(value): return round(value * PX_CM) def picture(path): """The small picture the Site sends of a file: WebP within 480 x 720 px.""" with Image.open(path) as image: image.thumbnail((480, 720)) out = io.BytesIO() image.save(out, 'WEBP', quality=80) return out.getvalue() class ProofRenderTests(unittest.TestCase): def _sheet(self, folder, bands, height_cm=30): """A 20 cm wide sheet painted in [(from_cm, colour)] bands.""" image = Image.new('RGBA', (cm(20), cm(height_cm)), bands[0][1]) draw = ImageDraw.Draw(image) for start, colour in bands[1:]: draw.rectangle((0, cm(start), cm(20) - 1, cm(height_cm) - 1), fill=colour) path = Path(folder) / 'sheet.png' image.save(path, 'PNG', dpi=(300, 300)) return str(path) def proof(self, bands, height_cm=30, fraction='0.75'): with tempfile.TemporaryDirectory() as source, tempfile.TemporaryDirectory() as out: sheet = self._sheet(source, bands, height_cm) parts, detail = render(item(height_cm, fraction), {0: (sheet, 'sheet.png')}, out, 'proof', limit_cm=15, proof_fraction=fraction, pictures={0: picture(sheet)}) self.assertEqual(len(parts), 1) with Image.open(parts[0][0]) as image: return image.convert('RGBA'), detail def test_the_window_follows_the_finest_stroke_and_samples_the_rest(self): # The stroke is in the blue half: its 15 cm window, then a 5 cm red # sample from above, 1 cm of clear film between them. image, detail = self.proof([(0, RED), (15, BLUE)]) self.assertEqual(detail['proof_windows'], [['0', '5'], ['15', '30']]) self.assertEqual(image.width, cm(20)) self.assertLessEqual(abs(image.height - cm(21)), 2) self.assertEqual(image.getpixel((image.width // 2, cm(2))), RED) self.assertEqual(image.getpixel((image.width // 2, cm(5.5)))[3], 0) self.assertEqual(image.getpixel((image.width // 2, cm(10))), BLUE) def test_the_top_is_still_the_window_without_a_measure(self): image, detail = self.proof([(0, RED), (15, BLUE)], fraction=None) self.assertEqual(detail['proof_windows'][0], ['0', '15']) self.assertEqual(image.getpixel((image.width // 2, cm(5))), RED) self.assertEqual(image.getpixel((image.width // 2, cm(18))), BLUE) def test_without_pictures_the_proof_is_the_stroke_window(self): with tempfile.TemporaryDirectory() as source, tempfile.TemporaryDirectory() as out: sheet = self._sheet(source, [(0, RED), (15, BLUE)]) _, detail = render(item(), {0: (sheet, 'sheet.png')}, out, 'proof', limit_cm=15, proof_fraction='0.75') self.assertEqual(detail['proof_windows'], [['15', '30']]) def test_one_colour_keeps_the_15_cm_proof(self): image, detail = self.proof([(0, BLUE)]) self.assertEqual(detail['proof_windows'], [['15', '30']]) self.assertLessEqual(abs(image.height - cm(15)), 1) def test_every_colour_within_30_cm(self): # Red, blue and green in a 100 cm sheet, the stroke in the blue. image, detail = self.proof([(0, RED), (30, BLUE), (60, GREEN)], height_cm=100, fraction='0.45') windows = [(float(a), float(b)) for a, b in detail['proof_windows']] self.assertEqual(len(windows), 3) self.assertLessEqual(sum(b - a for a, b in windows) + 1 * (len(windows) - 1), 30) colours = {image.getpixel((image.width // 2, y)) for y in range(0, image.height, cm(1))} self.assertTrue({RED, BLUE, GREEN} <= colours, colours) if __name__ == '__main__': unittest.main()