ASCII-fying Paul Ford

I remember fondly printing out 132-column line printer ASCII drawings back in the old days. In a fit of nostalgic whimsy, I’ve put together a quick program to produce ASCII art from bitmap images in Racket:

(require 2htdp/image)

(define ascii-by-density (string->list "@%#*+=-:. "))

(define (choose-char cl w x y)
  (let* ([pixel (list-ref cl (+ x (* y w)))]
         [peak  (max (color-red pixel)
                     (color-green pixel)
                     (color-blue pixel))])
    (if (zero? peak)
        (last ascii-by-density)
        (let ([c (round (- (* (length ascii-by-density) (/ peak 255)) 1))])
          (if (positive? c)
              (list-ref ascii-by-density c)
              (first ascii-by-density))))))

(define (write-asciified-bitmap bm filename)
  (with-output-to-file filename
    (lambda ()
      (let ([w (image-width bm)]
            [cl (image->color-list bm)])
        (for ([y (range (image-height bm))])
          (for ([x (range w)])
            (display (choose-char cl w x y)))
          (display "\n"))))))

(define paul-ford (bitmap/url ""))

(write-asciified-bitmap paul-ford "ford.txt")

Demonstrated here using this photo of my friend Paul Ford:

Paul Ford, photo by Paul, ASCII by Racket

I hope he doesn’t mind.

This entry is part of Jack Rusher’s journal, originally published October 9th, 2014, in Berlin.