
Le principe :
on determine la force de luminosité des caracteres Ascii
soit par un algo super compliqué, soit en comptant le nombre de points composant chaque lettre
et se dire que plus il y a de points, plus c'est foncé !
ça nous donne une sorte de palette , du caractere le plus clair (chr(32) ... l'espace )
au caractere le plus foncé ....(M ou @ par exemple )
il suffit de classer notre palette de caractere dans un tableau ou une liste
ensuite on charge une image, et on determine le taux de luminosité de chaque pixel
(RVB)/3
il suffit ensuite de faire correspondre chaque caractere au point de l'image observé
un pixel de l'image renvoi 133 , il faut regarder dans notre palette de caractere , la valeur se raprochant le plus de 133
et utiliser ce caractere pour dessiner notre Ascii-art !
c'est tout con, et ça marche pas mal

apres on peux jouer sur la palette de caractere, le type de caractere, et meme pourquoi pas utiliser
des caracteres chinois ou autre, du moment que ces caracteres soient suffisament different pour representer une valeur de
luminosité differentes ...

le prg ressemble a ça : (un air de deja vue ? )

ne pas chercher a convertir des photos trop fouillé, forcement, on perd en definition
mais pour convertir des images simple, ça le fait

jouez avec le curseur pour obtenir des résultats différents

le résultat génère un text de 128 caractères X 128 lignes .....
le résultat est visible dans le prg ou par copier collé dans le notepad
pour le prg j'utilise une fonte tres petite
le code :
Code : Tout sélectionner
;***********************************************
;Titre :*ascii_art
;Auteur : Zorro
;Date :16/10/2016
;Heure :14:39:15
;Version Purebasic : PureBasic 5.50 (Windows - x64)
;Version de l'editeur :EPB V2.64
; Libairies necessaire : Aucune
;***********************************************
;***********************************************
;{- Enumerations / DataSections
;{ Windows
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UsePNGImageDecoder()
UsePNGImageEncoder()
Enumeration
#Win
#file
EndEnumeration
;}
;{ Gadgets
Enumeration
#Image_0
#Image_1
#editor
#Text_Titre
#Text_auteur
#Button_Load
#Button_Save
#Button_calcul
#progress_calcul
#image_origine
#image_origine_gadget
#image_origine_gadget2
#image_modifie
#image_modifie_gadget
#image_modifie2
#Text_version
#valeur
#batch_convert
#valeur_text
#buffer
#track_taille_fonte
#valeur_taille_fonte
#Text_fonte
#Text_tolerance
EndEnumeration
;}
;{ Fonts
Enumeration
#Font_Text_Titre
#Font_Text
EndEnumeration
;}
;{ Images
Enumeration
#Image_Image_0
#Image_Image_1
EndEnumeration
;}
Structure image_file
image_file.s
image_file_batch.s
repertoire_sortie.s
here.s
name_image.s
extens_image.s
Largeur_image.i
hauteur_image.i
vall.i
taille_fonte.i
EndStructure
Global image_file.image_file
Global Dim Image_or(1,1)
Global Dim Image_transp(1,1)
Global Dim Image_res(1,1)
Global Dim tab_car(256)
Global Version.s="1.00"
; **********************************
;-declaration Procedures
Declare openwindow_win()
Declare calcul()
Declare save(flag)
Declare load(flag)
Declare save_ini()
Declare load_ini()
Declare lumiere_car()
Global taille=128
Global Nom_fonte.s="Lucida Console"
image_file\taille_fonte.i=3
; **********************************
Define.l Event
;}
Load_ini()
OpenWindow_Win()
lumiere_car()
;{- Event loop
Repeat
Event = WaitWindowEvent(12)
Select Event
; ///////////////////
Case #PB_Event_Gadget
Select EventGadget()
Case #Image_0
Case #Image_1
Case #track_taille_fonte
image_file\taille_fonte.i=getGadgetstate(#track_taille_fonte)
if Not LoadFont(#Font_Text, Nom_fonte.s, image_file\taille_fonte.i, #PB_Font_HighQuality)
MessageRequester("erreur","pas pu charger la fonte")
End
Endif
SetGadgetText(#valeur_taille_fonte,str(image_file\taille_fonte.i))
Calcul()
Case #batch_convert
;-bath_convert
image_file\image_file_batch.s=OpenFileRequester("choisir Les images a convertir",image_file\here.s,"*.jpg;*.png;*.bmp",1,#PB_Requester_MultiSelection)
image_file\repertoire_sortie.s=PathRequester("indiquez le repertoire de sortie",image_file\here.s)
While image_file\image_file_batch.s
;Debug image_file\image_file_batch.s
SetWindowTitle(#win,"calcul img:"+image_file\image_file_batch.s)
image_file\image_file.s=image_file\image_file_batch.s
;load image en cours
Load(#true)
;calcul
Calcul()
image_file\image_file_batch.s = NextSelectedFileName()
save(#True)
Wend
Case #Text_Titre
Case #Text_auteur
Case #Button_Load
;-load
Load(#false)
Case #Button_Save
;-Save
save(#false)
Case #Button_calcul
;-calcul
image_file\taille_fonte.i=getGadgetstate(#track_taille_fonte)
if Not LoadFont(#Font_Text, Nom_fonte.s, image_file\taille_fonte.i, #PB_Font_HighQuality)
MessageRequester("erreur","pas pu charger la fonte")
End
Endif
Calcul()
Case #valeur
image_file\vall.i=getGadgetstate(#Valeur)
SetGadgetText(#valeur_text,str(image_file\vall.i))
EndSelect
; ////////////////////////
Case #PB_Event_CloseWindow
Select EventWindow()
Case #Win
Save_ini()
CloseWindow(#Win)
Break
EndSelect
EndSelect
Forever
;
;}
;- Procedures Zone
Procedure OpenWindow_Win()
;-openwindow
If OpenWindow(#Win, 271, 98, 600, 550, "Ascii-art by Zorro", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
;;If CreateGadgetList(WindowID(#Win))
ImageGadget(#Image_0, 230, 10,taille, taille/2, 0, #PB_Image_Border)
; ImageGadget(#Image_1, 230, 270, 325, 235, 0, #PB_Image_Border)
EditorGadget(#editor,100,160,490,380)
TextGadget(#Text_Titre, 10, 15, 100, 40, "Ascii-art")
TextGadget(#Text_version, 10, 55, 50, 20,Version.s )
TextGadget(#Text_auteur, 120, 15, 50, 20, "By Zorro")
ButtonGadget(#Button_calcul, 10, 375, 55, 50, "Calcul")
ProgressBarGadget(#progress_calcul, 10, 140,150, 15, 1, 100)
ButtonGadget(#Button_Load, 10, 440, 80, 25, "Load")
ButtonGadget(#Button_Save, 10, 470, 80, 25, "Save")
ButtonGadget(#batch_convert,10, 500, 80, 25, "Batch_convert")
TextGadget(#Text_tolerance, 10, 80, 180, 20, "Tolerance")
TrackBarGadget(#valeur,10,100,150,30,1,100)
TextGadget(#valeur_text,200,100,30,30,str(image_file\vall.i))
setGadgetstate(#Valeur,image_file\vall.i)
DisableGadget(#batch_convert, #true)
; Gadget Fonts
TextGadget(#Text_fonte, 250, 80, 180, 20, "Taille fonte")
TrackBarGadget(#track_taille_fonte,250,100,150,30,1,10)
TextGadget(#valeur_taille_fonte,450,100,30,30,str(image_file\taille_fonte))
setGadgetstate(#track_taille_fonte,image_file\taille_fonte.i)
SetGadgetFont(#Text_Titre, LoadFont(#Font_Text_Titre, "Arial", 20, #PB_Font_HighQuality))
if Not LoadFont(#Font_Text, Nom_fonte.s, image_file\taille_fonte.i, #PB_Font_HighQuality)
MessageRequester("erreur","pas pu charger la fonte")
End
Endif
;EndIf
EndIf
EndProcedure
Procedure Calcul()
SetGadgetFont(#editor, FontID(#font_text))
Save_ini()
if IsImage(#image_origine)
; defisher
; ***************************************************
txt.s="[size=5]"+chr(10)+chr(13)
For y=1 to image_file\hauteur_image-1
For x=1 to image_file\Largeur_image-1
pixl=Image_or(x,y)
lum=(red(pixl)+green(pixl)+blue(pixl))/3 ; recup la moyenne
; calldebugger
For i=32 to 255 ; regarde dans le tableau des caracteres
if lum=0 and Image_transp(x,y)=255
txt.s=txt.s+chr(213); noir
flag=#true
Break
Endif
if lum/tab_car(i)<image_file\vall.i
car=i
if (car>32 and car<128) or car>160
txt.s=txt.s+chr(car)
Else ; car<=32 or car=255 ;or Image_transp(x,y)=0
txt.s=txt.s+chr(0160)
Endif
flag=#true
break
Endif
; ici en principe on a un caratere a afficher
Next i
if flag=#false:txt.s=txt.s+chr(0160):endif
if flag=#true :flag=#false:endif
car=0160
Next x
SetGadgetState(#progress_calcul, x)
WaitWindowEvent(2)
txt.s=txt.s+chr(10)+chr(13)
Next y
SetGadgetText(#editor,txt.s)
StopDrawing()
txt.s=txt.s+chr(10)+chr(13)+"[/size]"
SetClipboardText(txt.s)
; ***************************************************
SetGadgetState(#progress_calcul, 0)
DisableGadget(#batch_convert, #False)
Endif
Endprocedure
Procedure Save(flag)
if flag=#false
path_save.s=SaveFileRequester("ou sauver ?" ,image_file\here.s+image_file\name_image.s+"."+image_file\extens_image.s,"*.jpg;*.png;*.bmp",1)
Else
path_save.s=image_file\repertoire_sortie.s+image_file\name_image.s+"."+image_file\extens_image.s
Endif
OpenFile(#file,image_file\name_image.s+".txt")
For i=1 to CountGadgetItems(#editor)
ligne.s=GetGadgetItemText(#editor,i)
WriteStringN(#file,ligne$)
Next i
Endprocedure
Procedure Load(flag)
load_ini()
if flag=#false
image_file\image_file.s=OpenFileRequester("choisir une image",image_file\here.s,"*.jpg;*.png;*.bmp",1)
Endif
if image_file\image_file.s<>""
image_file\here.s=GetPathPart( image_file\image_file.s)
image_file\name_image.s=GetFilePart( image_file\image_file.s,#PB_FileSystem_NoExtension)
image_file\extens_image.s=GetExtensionPart( image_file\image_file.s)
If LoadImage(#image_origine, image_file\image_file.s)
CopyImage(#image_origine,#image_origine_gadget)
CopyImage(#image_origine,#image_origine_gadget2)
xim=ImageWidth(#image_origine)
yim=ImageHeight(#image_origine)
rappor.f=(xim/yim)*3
ResizeImage(#image_origine_gadget,taille,taille/rappor.f)
ResizeImage(#image_origine_gadget2,128,64)
image_file\Largeur_image=taille
image_file\hauteur_image=taille/rappor.f
SetGadgetState(#Image_0, imageId(#image_origine_gadget2))
Dim Image_or(image_file\Largeur_image,image_file\hauteur_image+10)
Dim Image_transp(image_file\Largeur_image,image_file\hauteur_image+10)
; Scan
StartDrawing(ImageOutput(#image_origine_gadget))
DrawingMode(#PB_2DDrawing_AlphaBlend )
For y=0 to image_file\hauteur_image-1
For x=0 to image_file\Largeur_image-1
col=point(x,y)
Image_or(x,y)=(col)
Image_transp(x,y)=alpha(col)
Next x
Next y
StopDrawing()
Else
MessageRequester("erreur", "pas pu charger cette image !")
Endif
Endif
EndProcedure
procedure Save_ini()
If Openfile(#file,GetCurrentDirectory()+"Pixeliser.ini")
;-save_ini
writeStringN(#file,image_file\here.s)
image_file\vall.i=getGadgetstate(#Valeur)
writeStringN(#file,str(image_file\vall.i))
image_file\taille_fonte.i=getGadgetstate(#track_taille_fonte)
writeStringN(#file,str(image_file\taille_fonte.i))
CloseFile(#file)
Else
image_file\here.s=GetCurrentDirectory()
Endif;]
EndProcedure
Procedure load_ini()
if FileSize(GetCurrentDirectory()+"Pixeliser.ini")>1
If Openfile(#file,GetCurrentDirectory()+"Pixeliser.ini")
image_file\here.s=ReadString(#file)
image_file\vall.i=val(ReadString(#file))
image_file\taille_fonte.i=val(ReadString(#file))
CloseFile(#file)
Endif
Else
image_file\here.s=GetCurrentDirectory();|
Endif
EndProcedure
Procedure lumiere_car()
CreateImage(#buffer,16,16,32,$1)
StartDrawing(ImageOutput(#buffer))
For i=32 to 255
car$=chr(i)
DrawText(1,1,car$,rgb(255,0,0),$1)
For y=1 to 15
For x=1 to 15
coul=point(x,y)
r=red(coul)
taux=taux+r
compteur_de_point=compteur_de_point+1
Next x
Next y
tab_car(i)=(taux/compteur_de_point)*255/100
Box(1,1,15,15,$1); efface
taux=0 :compteur_de_point=0
Next i
StopDrawing()
SortArray(tab_car(), #PB_Sort_Ascending , 32, 255)
Endprocedure
;Epb