Created
June 30, 2013 05:44
-
-
Save keshavsaharia/5894016 to your computer and use it in GitHub Desktop.
A Mathematica function for generating beautiful ASCII art out of any image.
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
(*ASCIIrange = { " ","` ",". "," .","..",".:",":.","::",":#","##","#@","x@","o@","@@","@X","XX"};*) | |
(*ASCIIrange = { " "," ",".","..",".:","::",":i","io","ex","pq","Xq","AG","ZY","VW"};*) | |
(* Produces the best results by randomizing letters using a held random choice call *) | |
ASCIIrange = {" ", Hold[RandomChoice[{" ", " `"}]], ". ", "..", | |
Hold[RandomChoice[{".:", ":."}]], | |
Hold[RandomChoice[{"::", ";:", ":;", ";;"}]], | |
Hold[RandomChoice[{":x", "x:"}]], | |
Hold[RandomChoice[{"io", "ae", "oc"}]], | |
Hold[RandomChoice[{"yj", "tp", "qw"}]], | |
Hold[RandomChoice[{"IL", "02", "JC"}]], | |
Hold[RandomChoice[{"GP", "ZY", "VW"}]], | |
Hold[RandomChoice[{"#@", "&%", "8#"}]]}; | |
maxthreshold = Length[ASCIIrange] - 2; | |
ASCIIart[image_, adjust_: {0.2, 1.5}, resolution_: 200, | |
threshold_: maxthreshold] := StringJoin[(StringJoin[#] <> "\n" & /@ (ReleaseHold /@ (Round /@ (threshold* | |
Rescale /@ Map[Mean[#] & /@ # &, ImageData[ | |
ImageResize[ImageAdjust[image, adjust], resolution], | |
"Byte"]]) /. MapThread [#1 -> #2 &, {Reverse[Range[0, threshold + 1]],Take[ASCIIrange, threshold + 2]}])))] /; (2 <= threshold <= maxthreshold && ImageQ[image]); | |
ASCIIimage[image_, adjust_: {0.2, 1.5}, resolution_: 200, | |
threshold_: maxthreshold] := Rasterize[Style[ASCIIart[image, adjust, resolution, threshold], Bold,FontFamily -> "Courier", FontSize -> 5]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment