&********
*%%%%%%%%%%%%%%%%%*
*%%%%%****%%%%%%%****&%%*
*%%%%%% *%%%% *%%%&*
%%%%%%& *%% *%%%%&
*%%%%%%% %* %%%%%%*
%%%%%%%%%* % &%%%%%%&
*%%%%%%%%%& * *%%%%%%%%
*%%%%%%%%%% %%%%%%%%%&
*%%%%%%%%%%% **& ** %%%%%%%%%%
*%%%%%%%%%%*% *** ** %%%%%%%%%
*%%%%%%%%&& * * * *%%%%%%*
*%%%%%%%& & * & *& * *%%%&&*
%%**%%%* % *** * %%%
*%%%%% * * *****& * * *%%%*
%%%%%& &* * * **** * * %%%%
*%%%%& * * * * **** * *%%*
%%%%% ******* * * **%%
*%%%%***** %****** *&%*
%%%%% * ***** %%
&%%%% * *********%
*%%%* * *%*
&%%%* * %*
%%%%* * * %*
%%%& * * * %*
%%%* * * %*
%%%* *% &&**& ** * **
%%%* * ** ************************* * * **
%%%* ** ************************************ **
%%%* * *************************************** %*
%%%* &************************************** **
*%%* ************************************** **
*%%* ************************************* **
%%& ************************************ *
%%% ************************************ **
*%% ********************************** %**
%%* &*********%%************%********& &* * *
*%* ******%******************%****** %** *
%% **************%*************** &** * *
*%* **%*************************% &**
%% *************************** %&** *
&%* %************************ &** * *
*% *********************** &&* %% %
&% ******************** &&***%%%
%* ***************%% &&**%%%%% *
&% *&********** &&&*%%%%%%%* *
%% ***** &&&*%%%%%%%%%%* *
*% ***%%%%%%%%%%%%*
*** ************%%%%%%%%%%%*
********************%*******************%%%%%%%%%%%
******************* &&&&&************** %%%%%%%%%%*
%%***************** * *&********%&%%%*%%%%%%%%**
&%%%%%%%%%%* *& * *&* &%%%%%%%%%%%%**
*%%%%%%%%%%* * & * &&* %%%%%&%%%%%***
*%%%%%%%%%%% *&&&%**&&& %%%%%%%**%
*%%%%%%%%%%%* &&&***&&* *%%%%&****
*%%%%%%&%%%%% *&&&*&&& %%%%***&
%%%%%%%%%%%% &&*&&& &%%%**
&%%%%%%%*%%%%* *** *%%%&
*%%%%%%%%*%%%%* *%%%*
****%%%%%%%*%%%% %%****** ** *%%%*
* *%%%%%*%%%% *%%%*
* *%%%**%%%% *%%%*
&%&**%%%% % %%%%*
* *&***%%%% * %%%%
* *** &%%%%& * %%%%
* * %%%%* * *%%%%
& %%%%% & %%%%%
%%%%%* & *%%%%%
%%%%%% * %%%%%&
* * *%%%%%%% * %%%%%%
%%%%%%%%* * %* &%%%%%%*
%%%%%%%%&* *%%%%%%%%*
%%%%%%%%%% * *%%%%%%%%%%*
&%%%%%%%%%%%%%** ** %%%%%%%%%%%%
&%%%%%%%%%%%%%%%%%*****%%%%%%%%%%%%%%
&%%%%%%%%%%%%%%%* ****%%%%%%%%%%%%%%
**%%%%%%%%%%%%%%** **%%%%%%%%%%%%%%% *
% * %%%%%%%%%%%%** * %%%%%%%%%&*** *
& &********&
%* *
* * *
*
* * *
& *
* &* &* **
%***%%%%%***% %***
要實(shí)現(xiàn)上面的功能還是比較簡單的执桌,先把三通道圖像轉(zhuǎn)為灰度圖像家厌,然后根據(jù)灰度值進(jìn)行替換扔仓。
def img2txt(path):
im = Image.open(path).convert('L').resize((100,100))
data = np.array(im)
lines = []
for i in range(100):
line = []
for j in range(100):
c = data[i,j]
if c>200:
x = ' '
elif c>150:
x = '&'
elif c>100:
x = '%'
else :
x = '*'
line.append(x)
lines.append(''.join(line))
return lines
除了把這些圖像轉(zhuǎn)為符號(hào)之外褐奥,我們還可以把這些符號(hào)轉(zhuǎn)為圖片。
#params:lines=text lines,size=font size,font = font type
def lines2img(lines,size=10,font ='simfang.ttf'):
im = Image.new('RGB',(1000,1000),(255,255,255))
font = ImageFont.truetype(font,10)
draw = ImageDraw.Draw(im)
for i,l in enumerate(lines):
for j,c in enumerate(l):
draw.text((j*10,i*10),c,font=font,fill=(0,0,0))
return im