image to ascii
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
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