fix: a film is cut into parts where its own pixels are clear, never through the art when it can be helped (#136)
Some checks failed
Build and deploy / Validate source (push) Successful in 9s
Build and deploy / Publish images (push) Has been cancelled
Build and deploy / Secret scan and release gate (push) Has been cancelled
Build and deploy / Integration suite on a real stack (push) Has been cancelled

The layout sees a ready sheet as one piece, so a part was cut at its length,
through the artwork. Where no gap between pieces is near the limit, the
film is read there: the cut falls in the middle of the nearest clear band of
3 mm, else of the widest clear band (#83's packed sheets have 2 mm between
rows), else on the row with the least ink. A part is never shorter than half
the limit, and the render limit allows one part more.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-27 14:11:15 -03:00
parent eb56c49016
commit 331e43aa14
4 changed files with 105 additions and 12 deletions

View File

@@ -60,13 +60,16 @@ STREAMED = ('.png', '.pdf')
def render_seconds(item):
"""The render limit of one item: RENDER_SECONDS for each part of its film."""
"""The render limit of one item: RENDER_SECONDS for each part of its
film, and one more for a film in parts: a cut moved back to a clear band
(#136) can leave one part more than the layout alone counts."""
production = item.get('production') or {}
try:
parts = len(part_cuts(production)) - 1 if production.get('placements') else 1
except (KeyError, TypeError, ValueError, ArithmeticError):
parts = 1
return RENDER_SECONDS * max(1, parts)
parts = max(1, parts)
return RENDER_SECONDS * (parts + 1 if parts > 1 else 1)
def generated_identity(order_id):

View File

@@ -21,8 +21,9 @@ holds a whole sheet: the film is composed and written in bands of rows.
A layout longer than 15 m leaves as parts of up to 15 m (Dropstar,
2026-10-30), DTF-7-item-1-p3.png, each with its footer saying which part it
is. Loose artwork is cut where no piece crosses; a finished sheet is cut
between copies when it can be, and through the artwork when not.
is. Loose artwork is cut where no piece crosses; a finished sheet between
copies, or else in a clear band of its own pixels, and through the artwork
only when it has none (#136).
"""
import io
import math
@@ -389,8 +390,16 @@ class Piece:
# ---- Parts and footer -------------------------------------------------------
def part_cuts(production, part_cm=None):
"""Where the film is cut into parts, in cm from its top: [0, ..., height]."""
# A cut needs this much clear film: it then falls in the middle of it.
CLEAR_CUT_CM = Decimal('0.3')
def part_cuts(production, part_cm=None, clear=None):
"""Where the film is cut into parts, in cm from its top: [0, ..., height].
A cut falls in a gap between pieces; where the layout has none, as inside
a ready sheet (one piece to it), `clear(from_cm, to_cm)` finds a clear band
in the film's own pixels, so no artwork is cut in two (Marcus, 2026-11-27,
#136). Only without one either is the cut at the part's length."""
limit = Decimal(str(part_cm or PART_CM))
height = Decimal(str(production['height_cm']))
spans = [(Decimal(str(p['y_cm'])), Decimal(str(p['y_cm'])) + Decimal(str(p['length_cm'])))
@@ -401,11 +410,46 @@ def part_cuts(production, part_cm=None):
free = [end for _, end in spans
if start + limit - CUT_SEARCH_CM <= end <= start + limit
and not any(top < end < bottom for top, bottom in spans)]
cuts.append(max(free) if free else start + limit)
# The search never goes back past half a part: no part comes out short.
found = clear(max(start + limit / 2, start + limit - CUT_SEARCH_CM), start + limit) if clear and not free else None
cuts.append(max(free) if free else found if found and found > start else start + limit)
cuts.append(height)
return cuts
def clear_band(pieces, width, from_cm, to_cm):
"""Where to cut the film between the two, in cm, so the artwork suffers
least (Marcus, 2026-11-27, #136): the middle of the clear band of at least
CLEAR_CUT_CM nearest `to_cm`; else of the widest clear band there is (a
sheet packed edge to edge may have only 2 mm between rows); else the row
with the least ink. None when the stretch is empty of rows."""
top = px(from_cm)
ink_rows, amounts = [], []
for _, band in film_bands(pieces, top, px(to_cm), width):
ink = band.getchannel('A').point(lambda a: 255 if a > 8 else 0)
ink_rows.extend(ink.getprojection()[1])
amounts.extend(ink.resize((1, ink.height), Image.BOX).tobytes())
if not ink_rows:
return None
runs, end = [], None # (length, last row, first row), clear rows only
for row in range(len(ink_rows) - 1, -2, -1):
if row >= 0 and not ink_rows[row]:
end = row if end is None else end
continue
if end is not None:
runs.append((end - row, end, row + 1))
end = None
to_cm_of = lambda row: Decimal(top + row) / Decimal(str(PX_PER_CM))
need = px(CLEAR_CUT_CM)
wide = [run for run in runs if run[0] >= need]
if wide or runs:
# runs go from the bottom up: the first wide one is the nearest the limit.
length, last, first = wide[0] if wide else max(runs, key=lambda run: (run[0], run[1]))
return to_cm_of((first + last) // 2)
least = min(amounts)
return to_cm_of(max(row for row, amount in enumerate(amounts) if amount == least))
def proof_cut(height_cm, limit_cm, fraction=None):
"""A proof's window, in cm from the film's top: `limit_cm` centred on the
thinnest stroke (fraction 0 at the top, 1 at the bottom), clamped inside
@@ -591,6 +635,10 @@ def render(item, files, out_dir, prefix, footer=None, part_cm=None, tiles=False,
vector = {index for index, source in enumerate(sources) if source.pdf}
lowest = check_layout(item, [source.size for source in sources], vector, proof=bool(limit_cm))
width = px(production['film_width_cm'])
def laid_out():
return sorted((Piece(placement, sources[placement['source_index']])
for placement in production['placements']), key=lambda piece: piece.y)
windows = None
if limit_cm:
# A proof: the finest stroke's window and samples of the other
@@ -601,9 +649,16 @@ def render(item, files, out_dir, prefix, footer=None, part_cm=None, tiles=False,
film_scan(production, pictures))
cuts = [windows[0][0], windows[-1][1]]
else:
cuts = part_cuts(production, part_cm)
pieces = sorted((Piece(placement, sources[placement['source_index']])
for placement in production['placements']), key=lambda piece: piece.y)
def clear(from_cm, to_cm):
# Read on pieces of its own: the film is read forward only.
scanned = laid_out()
try:
return clear_band(scanned, width, from_cm, to_cm)
finally:
for piece in scanned:
piece.close()
cuts = part_cuts(production, part_cm, clear)
pieces = laid_out()
count = len(cuts) - 1
parts, manifests = [], []
for number in range(count):

View File

@@ -166,6 +166,40 @@ class PngRenderTests(unittest.TestCase):
self.assertEqual(second.convert('RGBA').getpixel((10, px(22) - px(21) + 5))[:3], RED)
self.assertEqual([p['name'] for p in detail['parts']], ['DTF-7-item-1-p1.png', 'DTF-7-item-1-p2.png'])
def sheet_spec(self, height_cm, film=10):
spec = item([placement(0, 0, film, height_cm)], [source(film, height_cm)], height_cm, film=film)
spec['production']['sources'][0]['kind'] = 'sheet'
return spec
def test_a_ready_sheet_is_cut_in_a_clear_band_not_through_the_art(self):
# Marcus, 2026-11-27, #136: one ready sheet is one piece to the layout;
# the cut finds the clear band in its pixels (30 to 31 cm) under 35 cm.
sheet = Image.new('RGBA', (px(10), px(40)), (220, 30, 60, 255))
sheet.paste((0, 0, 0, 0), (0, px(30), px(10), px(31)))
parts, detail = self.render(self.sheet_spec(40), [self.save(sheet, 'sheet.png')], part_cm=35)
cut = float(detail['parts'][0]['to_cm'])
self.assertTrue(30 < cut < 31, cut)
first = Image.open(parts[0][0]).convert('RGBA')
self.assertEqual(first.getpixel((10, first.height - 2))[3], 0) # ends in the clear band
def test_a_packed_sheet_is_cut_in_its_widest_clear_band(self):
# A sheet packed edge to edge (#83: 2 mm at most between rows): the
# widest clear band, here 2 mm between dots, still cuts no ink.
sheet = Image.new('RGBA', (px(10), px(40)), (220, 30, 60, 255))
sheet.paste((0, 0, 0, 0), (0, px(30), px(10), px(31)))
dots = [px(30 + tenth / 10) for tenth in range(1, 10, 2)] # a dot every 2 mm
for row in dots:
sheet.putpixel((px(5), row), (0, 0, 0, 255))
_, detail = self.render(self.sheet_spec(40), [self.save(sheet, 'sheet.png')], part_cm=35)
cut = px(float(detail['parts'][0]['to_cm']))
self.assertTrue(px(30) < cut < px(31) and cut not in dots, cut)
def test_without_any_clear_row_the_cut_takes_the_least_ink(self):
sheet = Image.new('RGBA', (px(10), px(40)), (220, 30, 60, 255))
sheet.paste((0, 0, 0, 0), (px(1), px(32), px(10), px(32) + 3)) # only a thin edge of ink there
_, detail = self.render(self.sheet_spec(40), [self.save(sheet, 'sheet.png')], part_cm=35)
self.assertTrue(px(32) <= px(float(detail['parts'][0]['to_cm'])) < px(32) + 3, detail['parts'][0])
def test_pieces_for_the_viewer_come_with_the_file(self):
art = quadrants(118, 59, alpha=True)
spec = item([placement(0, 0, 1, 0.5)], [source(1, 0.5)], 0.5)

View File

@@ -48,8 +48,9 @@ class RenderLimitTests(unittest.TestCase):
return {'production': {'height_cm': height, 'placements': [
{'y_cm': 0, 'length_cm': height, 'source_index': 0}]}}
self.assertEqual(render_seconds(film(800)), RENDER_SECONDS)
self.assertEqual(render_seconds(film(2359.2)), 2 * RENDER_SECONDS)
self.assertEqual(render_seconds(film(4600)), 4 * RENDER_SECONDS)
# A film in parts gets one part more: a cut moved to a clear band (#136).
self.assertEqual(render_seconds(film(2359.2)), 3 * RENDER_SECONDS)
self.assertEqual(render_seconds(film(4600)), 5 * RENDER_SECONDS)
self.assertEqual(render_seconds({'mode': 'file'}), RENDER_SECONDS)