Image to ASCII

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
Demivec
Addict
Addict
Posts: 4090
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Image to ASCII

Post by Demivec »

@Ideasvacuum: You have to compile it with v3.94 or before to compile it without any modifications.

Here's my update version:

Code: Select all

EnableExplicit
;Author: konne
;Update to PureBasic v5.73 LTS by Demivec 4/10/2021
UseJPEGImageDecoder()
UsePNGImageDecoder()


Structure Buchstaben
  P.b[104]
EndStructure

Global Dim A.Buchstaben(255)

Enumeration Fonts 1
  #fnt_Conversion
EndEnumeration

LoadFont(#fnt_Conversion,"Lucida Console",10)

Enumeration Windows 1
  #Window_0
EndEnumeration
;
Enumeration Gadgets 1
  #gdt_ProgressBar_prb
  #gdt_ImageView_img
  #gdt_intensity_tbv 
  #gdt_dark_txt
  #gdt_light_txt
  #gdt_changeIntensity_txt
  #gdt_progressPercent_txt
  #gdt_CreateASCII_btn
  #gdt_LoadImage_btn
  #gdt_SaveASCII_btn
  #gdt_ShowASCII_btn
  #gdt_bwImagePreview_btn
EndEnumeration

Enumeration Images 1
  #img_initLetters
  #img_source
  #img_working
EndEnumeration

Enumeration fileID 1
  #fid_inputFile
  #fid_outputFile
EndEnumeration

Procedure InitLetters()
  Protected i,s, x, y
  
  CreateImage(#img_initLetters,8,13)
  
  StartDrawing(ImageOutput(#img_initLetters))
  For i = 1 To 255
    
    Box(0,0,8,13,RGB(255,255,255))
    DrawingFont(FontID(#fnt_Conversion))
    DrawText(0, 0, Chr(i)) 
    
    For s=0 To 103
      x=s%8
      y=Int(s/8)
      
      If Point(x,y)=0
        A(i)\P[s]=1
      Else
        A(i)\P[s]=0 
      EndIf   
    Next 
    
  Next
  StopDrawing()
  
  FreeImage(#img_initLetters)
EndProcedure

Procedure.s MakeAscii(ImageID,Tolleranz)
  Protected W, H, Ende, Start, B.Buchstaben
  Protected y1, x1, Col, AltSame, Char, k, Same, s, x, y, u
  Protected pbx, pby, bx, by, obx, oby
  Protected String.s, Durch
  
  W=ImageWidth(ImageID)
  H=ImageHeight(ImageID)
  Ende=W*H/103
  ;MessageRequester("Durchgänge","Es werden um die "+Str()+" Durchgänge benötigt")
  
  Start=ElapsedMilliseconds()
  
  StartDrawing(ImageOutput(ImageID))
  
  H - 1 ;reduce by 1 to convert from one-based to zero-based offset counts
  W - 1
  pbx = W % 8 ;partial pattern size in pixels at last row or column
  pby = H % 13
  bx = W -  pbx ;pixel offsets for last whole row or column pattern
  by = H - pby

  For y1 = 0 To H Step 13
    If y1 < by: oby = #False: Else: oby = #True: EndIf
    For x1= 0 To W Step 8
      If x1 < bx: obx = #False: Else: obx = #True: EndIf
      
      For s=0 To 103
        x=s%8
        y=Int(s/8)
        
        If (obx And x > pbx) Or (oby And y > pby)
          B\P[s] = 0 ;weiß
        Else  
          Col=Point(x+x1,y+y1)
          
          If Red(Col)+Green(Col)+Blue(Col)>Tolleranz
            B\P[s]=0  ;weiß
          Else
            B\P[s]=1  ;schwarz
          EndIf 
        EndIf
      Next
      
      AltSame=-1
      Char=0
      
      For k = 1 To 255
        Same=0
        For u=0 To 103
          If B\P[u]=A(k)\P[u]
            Same+1
          EndIf
        Next
        
        If Same > AltSame       
          AltSame=Same
          Char=k
        EndIf
      Next
      
      String.s+Chr(Char) 
      Durch+1
      If Durch%20 = 5
        SetGadgetState(#gdt_ProgressBar_prb,Int((Durch/Ende)*100))
        SetGadgetText(#gdt_progressPercent_txt,Str(Durch)+" of "+Str(Ende)+" loops "+Str(Int((Durch/Ende)*100))+" %")
        WindowEvent()
      EndIf
      
    Next
    String.s + #CRLF$
  Next
  
  StopDrawing()
  
  Beep_(100,100)
  SetGadgetState(#gdt_ProgressBar_prb,Ende/Durch*100)
  SetGadgetText(#gdt_progressPercent_txt,"Finished")
  
  ProcedureReturn String.s
EndProcedure

Procedure Window()
  If OpenWindow(#Window_0, 542, 196, 382, 423, "Image-ASCII Converter",  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar )
    ProgressBarGadget(#gdt_ProgressBar_prb, 3, 393, 279, 24, 0, 100)
    TextGadget(#gdt_progressPercent_txt, 3, 312, 279, 13, "0%",#PB_Text_Center)
    
    ButtonGadget(#gdt_CreateASCII_btn, 291, 393, 84, 24, "Create ASCII")
    CreateImage(#img_working,285, 273)
    StartDrawing(ImageOutput(#img_working))
    Box(0,0,285,273,$FFFFFF)
    DrawText(285/2-TextWidth("Please load an image...")/2,120, "Please load an image...")       
    StopDrawing() 
    ImageGadget(#gdt_ImageView_img, 3, 30, 285, 273, ImageID(#img_working),  #PB_Image_Border )
    TrackBarGadget(#gdt_intensity_tbv, 297, 21, 30, 294, 0, 733, #PB_TrackBar_Vertical)
    SetGadgetState(#gdt_intensity_tbv,733/2)
    TextGadget(#gdt_dark_txt, 327, 27, 51, 27, "dark")
    TextGadget(#gdt_light_txt, 327, 294, 54, 21, "light")
    TextGadget(#gdt_changeIntensity_txt, 3, 3, 372, 18, "Here you can chance the black and white intesity")
    ButtonGadget(#gdt_LoadImage_btn, 6, 360, 87, 27, "Load image")
    ButtonGadget(#gdt_SaveASCII_btn, 99, 360, 87, 27, "Save ASCII")
    ButtonGadget(#gdt_ShowASCII_btn, 192, 360, 87, 27, "Show ASCII")
    ButtonGadget(#gdt_bwImagePreview_btn, 6, 330, 273, 27, "black-white-image preview")
  EndIf
EndProcedure

Procedure MakeImageBAW(Image,Tolleranz)
  Protected w, h, x, y, Col
  
  w=ImageWidth(Image)-1
  h=ImageHeight(Image)-1
  
  StartDrawing(ImageOutput(Image))
  For x = 0 To w
    For y = 0 To h
      Col=Point(x,y)
      If Red(Col)+Green(Col)+Blue(Col)>Tolleranz
        Plot(x,y,$FFFFFF)  ;weiß
      Else
        Plot(x,y,0)   ;schwarz
      EndIf 
    Next
  Next
  StopDrawing()
EndProcedure

Procedure LoadBild()
  Protected Para.s, Req.s
  
  Para.s=ProgramParameter()
  If Para.s
    Req.s=Para.s 
  Else
    Req.s=OpenFileRequester("Choose an image","D:\Bilder\Schöne Frauen\","Image |*.bmp;*.png;*.jpg",0)
  EndIf
  
  If Req<>""
    If IsImage(#img_source)
      FreeImage(#img_source)
    EndIf
    
    If LoadImage(#img_source,Req.s)=0
      MessageRequester("Error","The program wasn't able to load the image"+Chr(10)+"Name: "+Req.s)
      ProcedureReturn 0
    EndIf
  Else
    ProcedureReturn 0 
  EndIf
  
  ProcedureReturn 1
EndProcedure

Procedure SaveASCII(String.s,Pfad.s)
  If CreateFile(#fid_outputFile,Pfad.s)=0
    MessageRequester("Error","It wasn't possible to create a Cout-File."+Chr(10)+Pfad.s)
    ProcedureReturn
  EndIf
  WriteString(#fid_outputFile, String.s, #PB_Ascii)
  CloseFile(#fid_outputFile)
EndProcedure

Procedure Eventloop()
  Protected Event, WindowID, GadgetID, EventType
  Protected String.s, Wo.s, Name.s
  
  Repeat ; Start of the event loop
    
    Event = WaitWindowEvent() ; This line waits until an event is received from Windows
    
    WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
    GadgetID = EventGadget() ; Is it a gadget event?
    EventType = EventType() ; The event type
    
    ;You can place code here, and use the result as parameters for the procedures
    
    If Event = #PB_Event_Gadget
      
      Select GadgetID
        Case #gdt_ProgressBar_prb
          
        Case #gdt_CreateASCII_btn;erstellen
          If IsImage(#img_source)
            String.s=MakeAscii(#img_source,GetGadgetState(#gdt_intensity_tbv))
          Else
            MessageRequester("Error","You have to load an image first.")
          EndIf 
          ;MessageRequester("",String.s)
          
        Case #gdt_LoadImage_btn;laden
          If LoadBild()   
            StartDrawing(ImageOutput(#img_working))
            DrawImage(ImageID(#img_source),0,0,285, 273)
            StopDrawing()
            
            MakeImageBAW(#img_working,GetGadgetState(#gdt_intensity_tbv))         
            SetGadgetState(#gdt_ImageView_img,ImageID(#img_working))         
          EndIf
          
        Case #gdt_bwImagePreview_btn
          If IsImage(#img_source)=0
            MessageRequester("Error","You have to load an image first.")
          Else
            StartDrawing(ImageOutput(#img_working))
            DrawImage(ImageID(#img_source),0,0,285, 273)
            StopDrawing()
            
            MakeImageBAW(#img_working,GetGadgetState(#gdt_intensity_tbv))         
            SetGadgetState(#gdt_ImageView_img,ImageID(#img_working))
          EndIf
          
        Case #gdt_SaveASCII_btn;speichern
          If String.s=""
            MessageRequester("Error","You have to create ASCII first.")
          Else
            Wo.s=SaveFileRequester("Saverequester",GetCurrentDirectory(),"Text|*.txt",0)
            If Wo.s
              SaveASCII(String.s,Wo.s)
            EndIf 
          EndIf
          
        Case #gdt_ShowASCII_btn;sehen
          If String.s=""
            MessageRequester("Error","You have to create ASCII first.")
          Else         
            Name.s=GetCurrentDirectory()+"Ascii-Cout.txt"
            SaveASCII(String.s,Name.s)
            RunProgram("notepad.exe",Name.s,"")
          EndIf
          
      EndSelect
      
    EndIf
    
  Until Event = #PB_Event_CloseWindow ; End of the event loop
  
  End
EndProcedure

InitLetters()
Window()
Eventloop()
This differs from dige's in just some cosmetic ways and one important way. The correction needed to produce decent results was to output an ASCII file and not a unicode one.

I tried it on some more complex images as well and it still has some miss steps but it is working much better.

Here was the output from one simple test:

Code: Select all

??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
?????????????????????????????ÑѪª""¯""®Ñ?????????????????????????????????
?????????????????????????Ѫ""¾q¿"ªÑ?????????????????????????????
??????????????????????Ñ""???¿"®??????????????????????????
????????????????????²¾????,"Ñ???????????????????????
??????????????????®¶?????µ"Ñ?????????????????????
????????????????f¾??????Ç"Ñ???????????????????
??????????????@????????"??????????????????
?????????????ѳ????????L`?????????????????
????????????f¸¿,,??????????????????????????
???????????¨¬????????????????¶??????????????
??????????°Â???????????????????????????????
?????????È®ÑÑ®°??????????????????????????
????????®?????????????L"????????????
???????????????????????l???????????
???????È???????????????????????????
????????????????????????Â??????????
??????k¸?????????????????????????????
??????g??????????????????????????????
??????g????????????????????????????????
??????_g????????????????????????j?????????
??????¸____????????????????????????????????¶?????????
??????g?????????????????????????????????????????????????
??????¸????????????????????????????????????????????????????
??????g?????????????????????????????????????????????????????
??????k¸??????????????????????????????????????????????????????
???????¸???????????????????????????????????????????????????????
????????????????????????????????????????????????????Ê???????????
????????¶?????????????????????????????????????????????????????????
?????????????????????????????????????????????????????fg????????????
??????????????????????ʯ¯¶???????????????????????????????????????
??????????????????????Ê???????????????????????????????????????
????????????????????????¸???????????????????????????????????????
????????????.??????????????g__,????????????????????????????????????????
?????????????Çl???????????????????????????????????????f?????????????????
?????????????????????????????????????????????????????",??????????????????
????????????????y???????????????????????????????????®¸????????????????????
??????????????????¿"????????????????????????????????"¸g?????????????????????
????????????????????¿Ñ????????????????????????????f¸g???????????????????????
??????????????????????g¿"????????????????????????ª"¸g?????????????????????????
?????????????????????????µ¿_`ª????????????????Ñ®"¨¸,????????????????????????????
???????????????????????????????¿_¯²²¾ÑÑÑÑÑÑ®²"¸_¿g???????????????????????????????
?????????????????????????????????????gg??g????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
It should be noted that even though the above looks good, the unmodified version that was compiled with PureBasic v3.94 outputted an image with different characters even though everything was done with the same settings.
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Image to ASCII

Post by HeX0R »

two small changes:

Code: Select all

EnableExplicit
;Author: konne
;Update to PureBasic v5.73 LTS by Demivec 4/10/2021
UseJPEGImageDecoder()
UsePNGImageDecoder()

#MIN_CHAR = 32
#MAX_CHAR = 255

Structure Buchstaben
	P.b[104]
EndStructure

Global Dim A.Buchstaben(#MAX_CHAR)

CompilerIf #MAX_CHAR <= 255
Global BOM = #PB_Ascii
CompilerElse
Global BOM = #PB_UTF8
CompilerEndIf


Enumeration Fonts 1
	#fnt_Conversion
EndEnumeration

LoadFont(#fnt_Conversion, "Lucida Console", 10)

Enumeration Windows 1
	#Window_0
EndEnumeration
;
Enumeration Gadgets 1
	#gdt_ProgressBar_prb
	#gdt_ImageView_img
	#gdt_intensity_tbv
	#gdt_dark_txt
	#gdt_light_txt
	#gdt_changeIntensity_txt
	#gdt_progressPercent_txt
	#gdt_CreateASCII_btn
	#gdt_LoadImage_btn
	#gdt_SaveASCII_btn
	#gdt_ShowASCII_btn
	#gdt_bwImagePreview_btn
EndEnumeration

Enumeration Images 1
	#img_initLetters
	#img_source
	#img_working
EndEnumeration

Enumeration fileID 1
	#fid_inputFile
	#fid_outputFile
EndEnumeration

Procedure InitLetters()
	Protected i, s, x, y

	CreateImage(#img_initLetters, 8, 13)

	StartDrawing(ImageOutput(#img_initLetters))
	For i = #MIN_CHAR To #MAX_CHAR

		Box(0, 0, 8, 13, RGB(255, 255, 255))
		DrawingFont(FontID(#fnt_Conversion))
		DrawText(0, 0, Chr(i), 0, $FFFFFF)

		For s = 0 To 103
			x = s % 8
			y = Int(s / 8)

			If Red(Point(x, y)) + Blue(Point(x, y)) + Green(Point(x, y)) < 384
				A(i)\P[s] = 1
			Else
				A(i)\P[s] = 0
			EndIf
		Next

	Next

	StopDrawing()

	FreeImage(#img_initLetters)
EndProcedure

Procedure.s MakeAscii(ImageID, Tolleranz)
	Protected W, H, Ende, Start, B.Buchstaben
	Protected y1, x1, Col, AltSame, Char, k, Same, s, x, y, u
	Protected pbx, pby, bx, by, obx, oby
	Protected String.s, Durch

	W    = ImageWidth(ImageID)
	H    = ImageHeight(ImageID)
	Ende = W * H / 103
	;MessageRequester("Durchgänge","Es werden um die "+Str()+" Durchgänge benötigt")

	Start = ElapsedMilliseconds()

	StartDrawing(ImageOutput(ImageID))

	H - 1 ;reduce by 1 to convert from one-based to zero-based offset counts
	W - 1
	pbx = W % 8 ;partial pattern size in pixels at last row or column
	pby = H % 13
	bx  = W -  pbx ;pixel offsets for last whole row or column pattern
	by  = H - pby

	For y1 = 0 To H Step 13
		If y1 < by: oby = #False: Else: oby = #True: EndIf
		For x1 = 0 To W Step 8
			If x1 < bx: obx = #False: Else: obx = #True: EndIf

			For s = 0 To 103
				x = s % 8
				y = Int(s / 8)

				If (obx And x > pbx) Or (oby And y > pby)
					B\P[s] = 0 ;weiß
				Else
					Col = Point(x + x1, y + y1)

					If Red(Col) + Green(Col) + Blue(Col) > Tolleranz
						B\P[s] = 0  ;weiß
					Else
						B\P[s] = 1  ;schwarz
					EndIf
				EndIf
			Next


			AltSame = -1
			Char    = 0

			For k = #MIN_CHAR To #MAX_CHAR
				Same = 0
				For u = 0 To 103
					If B\P[u] = A(k)\P[u]
						Same + 1
					EndIf
				Next

				If Same > AltSame
					AltSame = Same
					Char    = k
				EndIf
				If AltSame = 104
					Break
				EndIf
			Next

			String.s + Chr(Char)
			Durch + 1
			If Durch % 20 = 5
				SetGadgetState(#gdt_ProgressBar_prb, Int((Durch / Ende)*100))
				SetGadgetText(#gdt_progressPercent_txt, Str(Durch) + " of " + Str(Ende) + " loops " + Str(Int((Durch / Ende)*100)) + " %")
				WindowEvent()
			EndIf

		Next
		String.s + #CRLF$
	Next

	StopDrawing()

	Beep_(100, 100)
	SetGadgetState(#gdt_ProgressBar_prb, Ende / Durch*100)
	SetGadgetText(#gdt_progressPercent_txt, "Finished")

	ProcedureReturn String.s
EndProcedure

Procedure Window()
	If OpenWindow(#Window_0, 542, 196, 382, 423, "Image-ASCII Converter",  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar )
		ProgressBarGadget(#gdt_ProgressBar_prb, 3, 393, 279, 24, 0, 100)
		TextGadget(#gdt_progressPercent_txt, 3, 312, 279, 13, "0%", #PB_Text_Center)

		ButtonGadget(#gdt_CreateASCII_btn, 291, 393, 84, 24, "Create ASCII")
		CreateImage(#img_working, 285, 273)
		StartDrawing(ImageOutput(#img_working))
		Box(0, 0, 285, 273, $FFFFFF)
		DrawText(285 / 2 - TextWidth("Please load an image...") / 2, 120, "Please load an image...")
		StopDrawing()
		ImageGadget(#gdt_ImageView_img, 3, 30, 285, 273, ImageID(#img_working),  #PB_Image_Border )
		TrackBarGadget(#gdt_intensity_tbv, 297, 21, 30, 294, 0, 733, #PB_TrackBar_Vertical)
		SetGadgetState(#gdt_intensity_tbv, 733 / 2)
		TextGadget(#gdt_dark_txt, 327, 27, 51, 27, "dark")
		TextGadget(#gdt_light_txt, 327, 294, 54, 21, "light")
		TextGadget(#gdt_changeIntensity_txt, 3, 3, 372, 18, "Here you can change the black and white intensity")
		ButtonGadget(#gdt_LoadImage_btn, 6, 360, 87, 27, "Load image")
		ButtonGadget(#gdt_SaveASCII_btn, 99, 360, 87, 27, "Save ASCII")
		ButtonGadget(#gdt_ShowASCII_btn, 192, 360, 87, 27, "Show ASCII")
		ButtonGadget(#gdt_bwImagePreview_btn, 6, 330, 273, 27, "black-white-image preview")
	EndIf
EndProcedure

Procedure MakeImageBAW(Image, Tolleranz)
	Protected w, h, x, y, Col

	w = ImageWidth(Image) - 1
	h = ImageHeight(Image) - 1

	StartDrawing(ImageOutput(Image))
	For x = 0 To w
		For y = 0 To h
			Col = Point(x, y)
			If Red(Col) + Green(Col) + Blue(Col) > Tolleranz
				Plot(x, y, $FFFFFF)  ;weiß
			Else
				Plot(x, y, 0)   ;schwarz
			EndIf
		Next
	Next
	StopDrawing()
EndProcedure

Procedure LoadBild()
	Protected Para.s, Req.s

	Para.s = ProgramParameter()
	If Para.s
		Req.s = Para.s
	Else
		Req.s = OpenFileRequester("Choose an image", "D:\Bilder\Schöne Frauen\", "Image |*.bmp;*.png;*.jpg", 0)
	EndIf

	If Req <> ""
		If IsImage(#img_source)
			FreeImage(#img_source)
		EndIf

		If LoadImage(#img_source, Req.s) = 0
			MessageRequester("Error", "The program wasn't able to load the image" + Chr(10) + "Name: " + Req.s)
			ProcedureReturn 0
		EndIf
	Else
		ProcedureReturn 0
	EndIf

	ProcedureReturn 1
EndProcedure

Procedure SaveASCII(String.s, Pfad.s)
	If CreateFile(#fid_outputFile, Pfad.s) = 0
		MessageRequester("Error", "It wasn't possible to create a Cout-File." + Chr(10) + Pfad.s)
		ProcedureReturn
	EndIf
	WriteStringFormat(#fid_outputFile, BOM)
	WriteString(#fid_outputFile, String.s, BOM)
	CloseFile(#fid_outputFile)
EndProcedure

Procedure Eventloop()
	Protected String.s, Wo.s, Name.s

	Repeat ; Start of the event loop

		Select WaitWindowEvent() ; This line waits until an event is received from Windows
			Case #PB_Event_Gadget

				Select EventGadget()
					Case #gdt_ProgressBar_prb

					Case #gdt_CreateASCII_btn;erstellen
						If IsImage(#img_source)
							String.s = MakeAscii(#img_source, GetGadgetState(#gdt_intensity_tbv))
						Else
							MessageRequester("Error", "You have to load an image first.")
						EndIf
						;MessageRequester("",String.s)
					Case #gdt_intensity_tbv
						If IsImage(#img_source)
							StartDrawing(ImageOutput(#img_working))
							DrawImage(ImageID(#img_source), 0, 0, 285, 273)
							StopDrawing()
							MakeImageBAW(#img_working, GetGadgetState(#gdt_intensity_tbv))
							SetGadgetState(#gdt_ImageView_img, ImageID(#img_working))
						EndIf

					Case #gdt_LoadImage_btn;laden
						If LoadBild()
							StartDrawing(ImageOutput(#img_working))
							DrawImage(ImageID(#img_source), 0, 0, 285, 273)
							StopDrawing()

							MakeImageBAW(#img_working, GetGadgetState(#gdt_intensity_tbv))
							SetGadgetState(#gdt_ImageView_img, ImageID(#img_working))
						EndIf

					Case #gdt_bwImagePreview_btn
						If IsImage(#img_source) = 0
							MessageRequester("Error", "You have to load an image first.")
						Else
							StartDrawing(ImageOutput(#img_working))
							DrawImage(ImageID(#img_source), 0, 0, 285, 273)
							StopDrawing()

							MakeImageBAW(#img_working, GetGadgetState(#gdt_intensity_tbv))
							SetGadgetState(#gdt_ImageView_img, ImageID(#img_working))
						EndIf

					Case #gdt_SaveASCII_btn;speichern
						If String.s = ""
							MessageRequester("Error", "You have to create ASCII first.")
						Else
							Wo.s = SaveFileRequester("Saverequester", GetCurrentDirectory(), "Text|*.txt", 0)
							If Wo.s
								SaveASCII(String.s, Wo.s)
							EndIf
						EndIf

					Case #gdt_ShowASCII_btn;sehen
						If String.s = ""
							MessageRequester("Error", "You have to create ASCII first.")
						Else
							Name.s = GetCurrentDirectory() + "Ascii-Cout.txt"
							SaveASCII(String.s, Name.s)
							RunProgram("notepad.exe", Name.s, "")
						EndIf

				EndSelect

			Case #PB_Event_CloseWindow
				Break
		EndSelect

	ForEver

	End
EndProcedure

InitLetters()
Window()
Eventloop()
which outputs:

Code: Select all

                                                                                           ¸__¿¿ammæææ&&&&&&&&&&&&&&æææmw=¿¿_¸         
                                                                                   ¸__¿mmæ&&&&¾@@ºº³f¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯[Îð¶¾&&&&        
                                                                              ¸_am&&&@@º³¯¯                 ¸_¿µm&&&&&&¾¾¾@@@f         
                                                                         ¸_æm&&¾@º¯¯                     _aæ&&@º³¯¯                    
                                                                     ¸¿mæ&¾@³¯                        ¸µ&&@f¯                          
                                                                  _aæ&@@¯¯                          ¸æ&@¯                              
                                                              ¸¿æ&&@f¯                             m&@'                                
                                                           ¸¿m&¾M¯                                m&f                                  
                                                         ,æ&¾º¯                                  &¾´                                   
                                                      ¸a&&@¯                                    á&&&&&&&&&æææmµ¿__¸                    
                                                    ,m&@¯                                      ¸¯¯¯¯¯¯¯¯¯¯¯¯³ºº@¾&&&æ¿_¸               
                                                 ¸¿æ&M¯                                        |  ¬-~   _          ¯º¾&&濸            
                                               ¸a&@f                                           C          ¯   ¬~.      ¯º¾&æ¿          
                                             ¸æ&@¯                                             L     ¬¿_     ¸__¿am&w¿__¸ ¯¾&æ¿        
                                           ¸æ&@¯                                               L     ¿_&&æ¿mæ&&&&&&&&&&&&濸 ¾&¾¸      
                                          m&@¯                                                 L     ¸ã&&&&&&&&&&&&&&&&&&&&&Ç '¾&y     
                                        ¿&@f                                                   C  _æ&&&&&&&&&&&&&&&&&&&&&&&&&¾  ¾&y    
                                      ,&&f                                                     |_m&&&&&&&&&&&&&&&&&&&&&&&&&&&&À  ¶&    
                                    ¸æ&M                                                       $&&&&&&&¾&&&&&&&&&&&&&&&&&&&&&&&L 3&C   
                                   ¿&@'                                 ¸__¿¿=mmææMººººªemw=a¿__&&&&&¾¾&&&&&&&&&&&&&&&&&&&º9&&&Ì  &|   
                                 ¸m&f                  Ç  ¿       ¸_amæ&&&&&&&&&@'     ,#¯  y&&&&&&&f  m&&&&&&&&&&&&&&&&&&  $&&&  &|   
                                µ&@´                   8&Ç$濸_æm&&@@º¯¯   &&&&f    ¸af    ¸&&&&&&&@   &&&&&&&&&&&&&&&&&&Î  3&&&  &C   
                              ¸m&f                     $&&&&&&¾@f¯        j&&@'   ,#¯      á&&&&&&&   ¾&&&&&&&&&&&&&&&&@    &&¾ j&¢   
                             y&@´                     ¸µ&&¾M¯            æ&&@    m&        &&&&&&&¾    Â&&&&@º@&&&&@¾&&@    j&&Î 8&C   
                            æ&M                    ¸aæ&¾f¯              æ&&@   ¸m&&Ç      j&&&&&&&8     ¯ºf´   ³&&         á&& &&   
                          ¸&&'                  ¸¿æ&@f                 æ&&@   ¸&&&&&     |&&&&&&&&             &&         ¸&&Î j&8    
                         ,&@´                 _æ&@f                   m&&@   ,&@&&&&¾     $&&&&&&&&L           u&&         &&@  &&C    
                        µ&@                ¸a&&M¯                    m&&@   ,&@ ã&&&&Ç    $&&&&&&&&&           j&&        æ&&C ¸&@     
                       m&f               ¸æ&@f                      ¸&&@   ¸&@   ¶&&&&    j&&&&&&&&&Ç          &&&Ç     ¸m&&Î  &¾      
                      æ&f              _æ&@'                        &&&  ¸&@     &&&&À   á&&&&&&&&&&&       ¸&&&&&Ç  ¸æ&&&@  u&f      
                    ¸&¾'             ,m&@¯                         m&&f   &8      '&&&&Çm  &&&&&&&&&&&&W¿___¿&&&&&&&æ&&&&&&C ¸&@       
                   ¸&@´            _m&@´                          ¸&&@ ¸ m&       Â&&&&H $&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&@  &8        
                  ¸&8            ¸æ&@´                            á&&C ´u&f         $&&&æ¢  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&8  æ¾´        
                 ¸&@           ¸æ&@'                             ¸&&@   &@           ¶&&&µ  $&&&&&&&&&&&&&&&&&&&&&&&&&&&&´ æ&'         
                ¸&@           ¿&@'                               á&&  j&            &&&¾¢  ¾&&&&&&&&&&&&&&&&&&&&&&&&&&f æ&'          
               ¸&8          ,&&f                                 &&@   &Î             3&&&DC '&&&&&&&&&&&&&&&&&&&&&&&&&f æ&'           
              ¸&8         ¸æ&@                                  j&&  j&              ¾&&8$  $&&&&&&&&&&&&&&&&&&&&&&&@ æ&'            
              &@         ,&@'                                   &&@   &@               $&&& ç  &&&&&&&&&&&&&&&&&&&&&&@ æ&'             
             æ&´   ¸   ¸æ&f                                    u&&C  j&                &&&C'Ç '&&&&&&&&&&&&&&&&&&&&@ æ&'              
            m&'  ,¸ æ¿_&@'                                     á&& ¸ &@                 &&&j $  ¶&&&&&&&&&&&&&&&&&&@¸#¾&Ç              
           µ&f    &æ&&&M                                       &&@   &¢                 &&&8  $  &&&&&&&&&&&&&&&&&@¸f  ¾&Ç             
          u&f     $&&¾'                                       u&&f  j&                u&&&8   Ç '&&&&&&&&&&&&&&&@y'    ¾&W            
         ¸&@      µ&@                                         j&&  m¾                 æ&&&Î   '. ¾&&&&&&&&&&&&&My       ³&¾           
         &¾      æ&f                                          8&¾   &|               ¸m&&&&     $ "¾&&&&&&&&&&&@#         '&&¸         
        m&    ¸&¾'                                           &&8 ´u&C             ¸æ&&&&&'      ¥ Ô&&&&&&&&&&Ǫ        \   ¾&¸        
       u&f    ,&@                                             &&¢  j&r          _¿m&&&&&@´        Ç $&&&&&&&&Øf          \   ¾&¸       
      ¸&@    µ&@                                             j&&C  |&     ¸_¿ææ&&&&&&&&f          ã  ¾&&&&&@û'      C     \   ¾&¸      
      á¾    m&K                                              j&&L  m&¸_¿mæ&&&&&&&&&&¾f             § '&&&&8¾     ¸  1          ¶&      
     j&f   æ&f                                               m&&   &&&&&&&&&&&&&&&@¯      , I       ,¶&&¾#      $Ç3|           &¾     
     &@   &¾'                                                &&&   &&&&&&&&&&¾@f¯          ÇC       |  &@f        ¶æj       `   '&Ç    
    á&  ¸&@´                                               _æ&&¾ ¸,@ffº¾&@f¯              ¸f        3  'C          &j        ¸   ¶&    
   u&f ¸&8                                               ¸m&@®º@##Mw¬¬¬L'Ç               ¿&          C  $          ¶&             &Ç   
   &8 ¸&@                                               ¸&@            ³/            ¸æ&&          1 ¸ C         $&C        L   m@   
  j&f¸&@                                                á&          |    $C           ,&&&¾              |         |&&        !   $&   
  &@¸&8                                                 ¶&          |    j@¸         µ&&&&8              j         j&&            j&L  
 j&Ç&@                                                  '&¾¸        ¢   ,´ ã        m&&&&&j             L3        ¸&&&            j&L  
 &@&¾                                                    g&&W=~~~~~a~~=C    ç      m&fj&&&¢             L3      ¸µ&&&@        ¸   |&   
j&&&'                                                   ¸&@         ç   ³¸  $     æ&f &&&&C             L3    _æ&&&&@         l   &¾   
&&&f                                                    á&          j    $  |    m&f j&&&&              j  ¿m&&&@f¯          ´  u&¢   
'@f                                                     ¶&          3    j  j   a&f  &&&&¾          ¸__u==¬@@@&f                 &&    
                                                        '&¾¸        j   ,´  C  µ&f  j&&&&Î       aª³³ª¬=~~=    ">            .  j&f    
                                                         '¾&æ==========*   u  u&@   &&&&&r      f                \             u&@     
                                                         m&f       $   ¾   | ¸&@   j&&&&¾      j                              ¸&8      
                                                         &Î            ç ¸ ¸&8    &&&&&Î       Ç                 L           &¾´      
                                                         &Ç         C   ¢_¢ &&´   m&&&&&         %¿______         L       ¸  æ&´       
                                                         ¶&¿          m&& j&f   ¸&&&&&Î        y"     ¯¯         ;       ´ æ&'        
                                                          ¾&æW=====@=m&&&Îu&Î    &&&&&&        u                  f        æ&'         
                                                            ¯¶&&&C   Î&&&&@    j&&&&&f                          C       æ&'          
                                                              &&&|    &&¾æ&   ¸&&&&&¾          ¾¸                     ¸&&'           
                                                              &&&&    ¾&&&f    &&&&&&C           ,º**¬¬=¬r             ¸&@´            
                                                              8&&&   $&&@    m&&&&&Ê           £                |    ¸f&&             
                                                              $&&&Ç    &&C   u&&&&&@            L               ¸   ¸f!$&            
                                                               &&&@    $&   u&&&&&@             ¥              ,@   ¸f l &C            
                                                               ¾&&&    &Ç ,&&&&&¾´              ³=L___¿¿¿___aæ&C  ,'  l &§            
                                                               $&&&Ç  ´ ¶&Q&&&&&&'                     &&&&&&&&@  µ´   l ¶&            
                                                               3&&&&     &&&&&&¾'                      &&&&&&&&  æ       j&           
                                           ¸¸¸_¸¸               &&&&C    ¶&&&&@´                       &&&&&&&f7#        3&C           
                                      ¸_am&&&&&&&&æw_          æ&&&&ã     &&&@                         &&&&&&¾ é          &|           
                                    ¿m&&@f¯¯ ¸¸¸ ¯³¾&æ       ,&&&&&&&L  ´ $&@                         3&&&&&&ÇZ           ¾8           
                                  ¿&&º¯  ¸¿m&&&&&m_ '&&      ¶&&@¶&&&¾     Ç                          3&&&&&@¢            ¶&           
                                ¸&&f   _æ&&&&&&&&&&y $&r      ¯¯¸æ&&&&L    '                           &&&&&@             3&C          
                               y&@´   æ&&&&º¯   '&&¢ &¾       ¸æ&&&&&&&     $                          &&&&&               &¢          
                              m&f   ¸&&&&M      ¸&&¸æ&'     _æ&&&&&&&&&Ç     ¥                         ¾&&&Î            L  ¶&          
                             m&f   ¸&&&&'      ¸&¾Ç&@'   ¸¿æ&&&&&&&&@&&&      Ç                        $&&&C               Â&C         
                            µ&f    &&&&´     ¸æ&&æ&M   _æ&&&&&&&&&f  $&&§   \ '¸                        &&&C            ì   &§         
                           ¸&@    j&&&'      &&&&M´ _m&&&&&&&&&@*     ¾&&C     ³                        Â&&§                $&        
                           &&     &&&&       ¾@f¸_æ&&&&&&&&&&@¯       '&&&      $                        ³&&Ç                &¾        
                           &Î     &&&&Ç     ¸_¿m&&&&&&&&&&@¯           $&&&      ¾                         ³¾æ_          ¸   ã&Ç       
                          j&C     $&&&&æwmmæ&&&&&&&&&&@º¯               ¶&&Ç      ⸠                                         ¶&       
                          3&;      ¾&&&&&&&&&&&&&&@º¯                    ¶&&Ç      ³Ç                                          &¾      
                           &&       ¯º@&&&&&&@ºf¯                         ¾&&¸   `  `Ç            ¡                       ´  ¸¸'&&     
                           '&&_mC        ¯                                 &&&¸       ª¸          ã                          ¯º¾&&Ç    
                            '¾&&Ǹ                  ¸¸__¿ammæææææmw¿_¸      ¾&&        `¾¸     .  &L                       ´   «&&&Ç   
                              ¯¾&&&æwµ¿¿_____¿¿¿mmæ&&&&&¾@ºº³fff³º@¾&&&æm&¿_¸¾&&¸        ¯m   ¸§¸ &C               ¸¸¸¸¸¸¸  ¸     $&W  
                                 ¯¯³º@¾¾&&&&&@¾@@ºf¯¯                 ¯¯º@¾&&&&&&¸        u   j&æm&| ¸¸¸____¿aæmæ&&&&&&&&&&&æWµ¿_¸¸³&¾ 
                                                                             ¯&&&&Ç       &&&&&&&&&&&&&&&&&&@@ººf¯¯¯     ¯¯¯fº@¾&&&ææ&}
                                                                               ¾&&&W    ¸æ&f¯¯¯¯¯¯¯¯¯¯¯                           ¯¯ºº´
                                                                                ³&&&æmæ&&@'                                            
                                                                                  ºººººº¯                                              
Last edited by HeX0R on Tue Apr 13, 2021 6:48 am, edited 1 time in total.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Image to ASCII

Post by IdeasVacuum »

Thanks guys! I will try it with my own sample images. It would have helped if we could have seen Konne's samples to know what would work best.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Image to ASCII

Post by IdeasVacuum »

You have to compile it with v3.94 or before to compile it without any modifications.
... I think that version was from Fred Flintstones days :D
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Image to ASCII

Post by IdeasVacuum »

Well, if you reduce the font height to 7 or less, the results are very good. The image needs a plain background and image size at least 1024 x 1024. Everyone's help much appreciated!
Image
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Image to ASCII

Post by HeX0R »

I've updated my code, because it seems none of us took antialiasing into account.
None of the letters have been "pixelized" correctly when you only check, if it is black or white.

Results are much better now.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Image to ASCII

Post by IdeasVacuum »

Great stuff Hexor!

I think a lot depends on the image too, white background and clean outline makes a difference.

I also made the Track Bar longer for finer control and changed the range to 0 - 1000.
Last edited by IdeasVacuum on Thu Apr 15, 2021 10:59 am, edited 1 time in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Demivec
Addict
Addict
Posts: 4090
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Image to ASCII

Post by Demivec »

HeX0R wrote:I've updated my code, because it seems none of us took antialiasing into account.
None of the letters have been "pixelized" correctly when you only check, if it is black or white.

Results are much better now.
You actually addressed several failures at once.

These were
  • - That the default colors for the foreground and background in older versions of PureBasic were opposite to what they are now (black instead of white and white instead of black)
    - The overlooking of the Antialiasing effect of drawn letters
    - The outputting of ASCII control characters by including them in the range of character images to match against
There are still some unused parts left in the interface and I may make some changes to those to remove them and replace them with some useful options, in addition to the one Ideasvacuum mentioned. If I do so, I'll just update my former posting with the code changes.
Post Reply