Skip to content
Hokusai2.2.2
ivantokar.com

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) / columns
let 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"))
  • Six square crops of the landscape in a grid on a paper-coloured canvas: west, centre, east, attention, entropy and north-east.
    sheet.webpWEBP · 708 × 476 · 22 KB
  • 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, .entropy and .northEast crops (resize position).
  • One composite call places all tiles with CompositeLayer(tile, x:y:) from plain arithmetic (gap + column × (cell + gap)). All layers are blended in one native operation.
  • Captions: add drawText layers 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 VectorPage with .image(_:frame:fit:) (PDF report).