Skip to content

Instantly share code, notes, and snippets.

@Indy9000
Created December 26, 2015 00:29
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 Indy9000/9e4ab0bc20e0587f3dff to your computer and use it in GitHub Desktop.
Save Indy9000/9e4ab0bc20e0587f3dff to your computer and use it in GitHub Desktop.
image to ascii
let Color2Gray (c:Color) =
//((float)c.R * 0.3) + ((float)c.G * 0.59) + ((float)c.B * 0.11)
((float)c.R * 0.2126) + ((float)c.G * 0.7152) + ((float)c.B * 0.0722)
let Image2Ascii (img:Image) =
let mutable n_h = 60
let mutable n_w = 80
n_h <- (int)((float)img.Height * (float)n_w/(2.1 *(float)img.Width)) //calculate resized height
if n_h > 60 then
n_h <- 60
n_w <- (int)((float)img.Width * 2.1 * (float)n_h/(float)img.Height)
use bmp = new Bitmap(img,new Size(n_w,n_h)) //resize
//let asciiMap = [|' ';'.';',';':';';';'o';'x';'%';'#';'@'|]
let asciiMap = [|'`';'\'';'.';',';':';';';'i';'+';'o';'*';'%';'&';'$';'#';'@'|]
for y in [|0 .. bmp.Size.Height-1|] do
for x in [|0 .. bmp.Size.Width-1|] do
//convert to grayscale
let grayScale = Color2Gray (bmp.GetPixel(x,y))
let scaled = (grayScale * float(Array.length(asciiMap)-1) /255.0)
let index = (int)(scaled + 0.5)
printf "%c" asciiMap.[index]
printfn ""
printfn ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment