Skip to content

About

Reference-image to HTML/CSS/SVG reconstruction with objective pixel, edge and palette metrics

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

image2code-bench

Reference-image → HTML/CSS/SVG reconstruction with objective pixel, edge and palette metrics.

CI Python License

This is a deterministic baseline for the "image-to-code" problem: given a reference screenshot or design, produce an inspectable HTML/CSS approximation and an SVG approximation, then measure how close each one is to the reference. It is built to make the evaluation honest — every number below is produced by the code in this repository, on synthetic fixtures, with no proprietary images.

This is an independent portfolio project. It is not affiliated with, endorsed by, or copied from any company. It was built to demonstrate front-end, graphics and Python tooling work: HTML/CSS, SVG/Canvas, Pillow, NumPy, Matplotlib, CairoSVG and SVGWrite.

What it does

  1. Loads a reference image, converts to RGB and resizes to a deterministic working edge (default 320 px).
  2. Quantizes the palette with Pillow's median-cut implementation (default 12 colours).
  3. Grids the image into cells and takes the modal colour per cell, then merges horizontal runs of equal colour so the output is not one div per pixel.
  4. Emits:
    • reconstruction.html — a self-contained HTML/CSS approximation made of absolutely positioned colour blocks (a pure-CSS first pass).
    • reconstruction.svg — one <rect> per merged run, with a viewBox.
    • index.html — a report with a Canvas comparison slider, reference / reconstruction / difference heatmap, the live HTML approximation and the SVG source.
  5. Measures MAE, RMSE, PSNR, global SSIM, Sobel edge IoU and RGB histogram intersection against the reference.

Quick start

python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"

image2code path/to/reference.png --out out --colors 16 --cell 4
open out/index.html

Measured output

On the repository's synthetic fixture (tests/conftest.py::blocks_image, four quadrants plus a centre circle, 160×160, 16 colours, 8 px cells):

Metric Value
Cells after run merging 849
MAE 12.1135
RMSE 38.2344
PSNR 16.4817 dB
Global SSIM 0.898613
Sobel edge IoU 0.315219
Palette similarity 0.864209

Reproduce with:

.venv/bin/python examples/make_fixture.py
.venv/bin/image2code examples/reference-ui.png --out out --colors 16 --cell 4

The edge IoU is deliberately reported even though it is low. A 4 px colour grid cannot reproduce anti-aliased edges; quoting only SSIM would hide that.

The table is intentionally not filled with expected numbers. Run the command and copy the real values from out/metrics.json. The same rule applies to any result quoted in a CV or README: measure first, then quote.

Metric definitions

  • MAE / RMSE — per-channel absolute and squared error, averaged over all pixels. Lower is better.
  • PSNR — 20 * log10(255 / RMSE). Higher is better; identical images return 999.0 in the JSON to keep the payload finite.
  • Global SSIM — the single-window SSIM formula, not a windowed SSIM. It is labelled "global" everywhere so it is not mistaken for the standard windowed implementation.
  • Sobel edge IoU — Sobel gradient magnitude on grayscale, thresholded at mean + std, then intersection-over-union. It measures structural agreement, not perceptual quality.
  • Palette similarity — histogram intersection over a 16×16×16 RGB histogram. It catches colour drift that MAE can hide.

Why a baseline?

A generative image-to-code model is hard to evaluate: outputs vary between runs, and "looks close" is not a metric. A deterministic baseline gives a stable, reproducible lower bound. If a future model cannot beat this baseline on MAE, SSIM and edge IoU, it has not earned the complexity. The harness is deliberately model-agnostic: any candidate renderer can be dropped in and scored with the same functions.

Limitations (read this before quoting results)

  • Not semantic. The HTML is a colour-block approximation, not a DOM with headings, buttons or text. It is a visual baseline, not a production UI generator.
  • Synthetic fixtures only. No proprietary screenshots, brand assets or customer data are included. The tests generate their own images.
  • Global SSIM. The implementation is single-window; do not compare it directly with windowed SSIM numbers from other papers.
  • CPU only. No GPU, no model weights, no training. This is intentionally a small, inspectable tool.
  • No claim of client work. This repository is a portfolio project. It is not a shipped product and no commercial outcome is claimed.

Tests and CI

pytest -q --cov=image2code --cov-report=term-missing
ruff check .

CI runs on Python 3.10, 3.11 and 3.12 with a 90% statement-coverage gate. Local coverage is 94% (248 statements, 16 missed); the uncovered lines are mostly CLI error branches. The suite covers palette extraction, grid coverage, run merging, SVG parseability, HTML block counts, metric edge cases (identical images, shape mismatches), CLI artifacts and determinism.

Project layout

image2code/
  imaging.py    # load, quantize, grid, merge, render
  htmlgen.py    # HTML/CSS approximation
  vectorgen.py  # SVG approximation
  metrics.py    # MAE, RMSE, PSNR, SSIM, edge IoU, palette similarity
  report.py     # self-contained HTML report with Canvas slider
  cli.py        # image2code entry point
tests/          # synthetic fixtures and deterministic assertions

About

Reference-image to HTML/CSS/SVG reconstruction with objective pixel, edge and palette metrics

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages