My hobby project #2 (ASCII art) lazarus / 39-Graphics ---------------------------------------------------------------------- anyone · 2020-10-06 12:27 Hi all, again. Further to my first Graphical hobby project two days ago, as promised, I have tweaked that "img2chr" program a little bit so that now it displays ASCII art from image file. The concept behind it is to get 16 different intensity values from 24-bit color pixel. As defined in MS docs for ToARGB(): QuoteThe byte-ordering of the 32-bit ARGB value is AARRGGBB. The most significant byte (MSB), represented by AA, is the alpha component value. The second, third, and fourth bytes, represented by RR, GG, and BB, respectively, are the color components red, green, and blue, respectively (I use back the trick in my original C# code to get the 16 different grey intensity values -- hopefully it is correct because I see it is working anyway) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i                         pixel := img.Colors[x,y];             r := (pixel.Red shr 8) and $00ff;             g := (pixel.Green shr 8) and $00ff;           b := (pixel.Blue shr 8) and $00ff;             newPix := ((r shl 16) or (g shl 8) or (b)) and $00ffffff; Then there is an array of 'pattern' to match the 16 different grey intensity value accordingly. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  pattern:array [0..15] of char = ('#','#','@','@','%','%',';',';','*','*',',',',','.','.',' ',' ');......             Write(pattern[(newPix div 1048576)]); The logic in my code assumes the input image file is always 24-bit. (Alpha channel in ARGB is ignored) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$h+}uses fpreadgif, fpreadpng, fpreadbmp, fpreadjpeg, fpimage, classes, sysutils, Crt; var img : TFPMemoryImage;    ReadFile : string;    pixel : TFPColor;    x, y:integer;    ratioX, ratioY:integer;  procedure Init;begin  ReadFile := ParamStr(1);  img := TFPMemoryImage.Create(0,0);end; procedure ReadImage;const  pattern:array [0..15] of char = ('#','#','@','@','%','%',';',';','*','*',',',',','.','.',' ',' ');var  r, g, b:byte;  newPix: integer;  readerClass: TFPCustomImageReaderClass;  reader: TFPCustomImageReader;  stream: TStream;begin   if FileExists(ReadFile) then   begin     stream := TFileStream.Create(ReadFile, fmOpenRead + fmShareDenyNone);     try     stream.Position := 0;       readerClass := TFPCustomImage.FindHandlerFromStream(stream).Reader;       if Assigned(readerClass) then begin         reader := readerClass.Create;         try           img.LoadFromStream(stream, reader);         finally           reader.Free;         end;          ratioY := img.Height div 30;         ratioX := img.Width div 90;        y:=0;         x:=0;          while (y');  end  else    try      Init;    ReadImage;      Dipose;    except      on e : exception do        writeln ('Error: ',e.message);    end;   WriteLn('Press Enter to quit...');  ReadLn;end.  The screen shot showing the output of this "img2asc.pp" program. Result might not be ideal. (You can change the pattern array to finetune the result): Enjoy! ---------------------------------------------------------------------- Handoko · 2020-10-06 12:38 It looks good. Thumbs up! ---------------------------------------------------------------------- Lulu · 2020-10-06 13:50 Cool, thanks you to share ! ---------------------------------------------------------------------- anyone · 2020-10-06 13:59 Thank you, @Handoko and @Lulu. We can even create ASCII art Video from MP4 (convert frame by frame) and use a frame delimiter (e.g. '$') to separate each frame in the .mp4.txt. It is fun to watch the playback of ASCII art Video if each fixed-width character is small enough (e.g. 3-point Courier font). I have done it in C# but I think I'll just stop here (for porting it to Free Pascal). ---------------------------------------------------------------------- Ñuño_Martínez · 2020-10-10 10:06 Cool.  Remembers me a lot to AAlib. ---------------------------------------------------------------------- anyone · 2020-10-14 11:33 Thanks @Ñuño_Martínez. Someone has already done the AAlib in 1997, much earlier than me....  Now I have a website specially for these projects (img2chr and img2asc). It is called http://asciiartonline.com (although it does not support image file uploading and instant ASCII art conversion, as its name implies.)