Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Created October 9, 2014 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackrusher/b3e8031d5c1cc14c6c52 to your computer and use it in GitHub Desktop.
Save jackrusher/b3e8031d5c1cc14c6c52 to your computer and use it in GitHub Desktop.
A quick piece of code to create an ascii version of a bitmap from the web, here demo'd with the face of my friend Paul Ford.
(require 2htdp/image)
(define ascii-by-density '(#\# #\A #\@ #\% #\$ #\+ #\= #\* #\: #\, #\. #\space))
(define (choose-char cl w h 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)]
[h (image-height bm)]
[cl (image->color-list bm)])
(for/list ([y (range h)])
(for/list ([x (range w)])
(display (choose-char cl w h x y)))
(display "\n"))))))
;; scaled down to 20% size
(define scaled-ford (scale 0.2 (bitmap/url "http://blog.arc90.com/wp-content/uploads/2013/07/Paul-Ford.jpg")))
(write-asciified-bitmap scaled-ford "ford.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment