Re: Image to ASCII
Posted: Mon Apr 12, 2021 1:14 pm
@Ideasvacuum: You have to compile it with v3.94 or before to compile it without any modifications.
Here's my update version:
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:
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.
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()
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????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????