Created
October 9, 2014 20:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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