Image Splitter — Ruby source
Split an image into an even grid of tiles — rows × columns with a live cut-line preview and per-tile or bulk PNG download. Runs entirely in your browser.
This is the Ruby implementation — the same logic the interactive tool runs, in a shareable, citable form.
# Image splitter — tile geometry — Ruby port of the image-splitter tool.
module ImageSplitter
# One cut of the grid: the source rectangle that becomes a tile. In the
# browser tool this rect is drawImage'd into its own canvas and encoded
# to PNG; the geometry here is the deterministic part of that pipeline.
Tile = Struct.new(:x, :y, :width, :height) do
def to_s = "src=(#{x}, #{y}) #{width}x#{height}"
end
# Split a width x height image into rows x cols tiles, row-major.
# Mirrors splitToTiles in typescript.ts: tileW = floor(width / cols),
# tileH = floor(height / rows) — Integer division IS Math.floor for
# positives, so a 1024px width over 3 cols yields 341px tiles and the
# last 1px column is dropped, exactly like the TS.
def self.split_tiles(width, height, rows, cols)
tile_w = width / cols
tile_h = height / rows
(0...rows).flat_map do |row|
(0...cols).map do |col|
Tile.new(col * tile_w, row * tile_h, tile_w, tile_h)
end
end
end
end
# 1024x768 image, 2 rows x 3 cols -> six 341x384 tiles.
ImageSplitter.split_tiles(1024, 768, 2, 3).each { |tile| puts tile }
Also available in 8 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →