Contact sheet
Goal. Lay out six tiles in a 3 × 2 grid on one image, for a preview strip or a comparison board. Inputs: scene.png. Needs: Hokusai 2.2.2.
let positions: [ResizePosition] = [.west, .center, .east, .attention, .entropy, .northEast]let cell = 220, gap = 12, columns = 3
// A blank paper-coloured canvas the size of the grid: one pixel, enlarged.let rows = (positions.count + columns - 1) / columnslet canvas = try Hokusai(raw: Data([248, 242, 230]), width: 1, height: 1, format: .rgb8) .resize(width: gap + columns * (cell + gap), height: gap + rows * (cell + gap), fit: .fill, kernel: .nearest)
// Each cell is the same photo cropped differently; place them with plain arithmetic.let source = try Hokusai(url: URL(fileURLWithPath: "scene.png"))var layers: [CompositeLayer] = []for (index, position) in positions.enumerated() { let tile = try source.resize(width: cell, height: cell, fit: .cover, position: position) layers.append(CompositeLayer(tile, x: gap + (index % columns) * (cell + gap), y: gap + (index / columns) * (cell + gap)))}try await canvas.composite(layers).webp(quality: 85).write(to: URL(fileURLWithPath: "sheet.webp"))
sheet.webpWEBP · 708 × 476 · 22 KB
How it works
Section titled “How it works”- A blank canvas is built from one paper-coloured pixel enlarged with
resize(fit: .fill, kernel: .nearest)to the grid size:gap + columns × (cell + gap)wide and the same for rows. - Each tile is the same source resized to a 220-pixel square with a different
position, so the grid doubles as a comparison of.west,.center,.east,.attention,.entropyand.northEastcrops (resize position). - One
compositecall places all tiles withCompositeLayer(tile, x:y:)from plain arithmetic (gap + column × (cell + gap)). All layers are blended in one native operation.
Variations
Section titled “Variations”- Captions: add
drawTextlayers under each tile (text). - Different photos per tile: build each tile from its own file; they are independent pipelines.
- As a PDF: put the same tiles on a
VectorPagewith.image(_:frame:fit:)(PDF report).