Generating ASCII art from an image

The function im2text(pxl, name) takes a jpeg, scales it by a factor of 1/pxl and outputs a txt file. If the filename is not specified it will name it by a random number. 
The grayscale gradient is achieved by the following ASCII symbols gradient : $@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`''. 

The text file is better viewed using Notepad++ or any text editor with a zoom feature.


The Matlab code :

 function []=im2text(pxl, name)  
 %converts a jpg into a txt file (name.txt) of precision 1/pxl  
 if ~exist('name','var')  
   name=num2str(rand);  
 end  
 Image=pixelate(pxl,'r');  
 if numel(size(Image))==3  
   ImageG=im2double(rgb2gray(Image));  
 else  
   ImageG=im2double(Image);  
 end  
 ImageG=ImageG.^0.3;  
 ImageT=fix((ImageG-mod(ImageG, 1/69))*1e4)/1e4;  
 range=0:0.0001:1;  
 range=unique(fix((range-mod(range, 1/69))*1e4)/1e4);  
 symbase='$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`''. ';  
 im2txt();  
 function [Image]=pixelate(pxl,r)  
 % pixelates an image by grouping pxl number of pixels without resizing it, add any another variable to  
 % enable resize  
 Image=imread(uigetfile('image.jpg'));  
 [m,n,p]=size(Image);  
 if r=='r'  
 Image=Image(1:m-mod(m,2),1:n-mod(n,2),1:p);  
 Image=Image(1:pxl:numel(Image(:,1,1)),1:pxl:numel(Image(1,:,1)),:);  
 else  
   for i=1:pxl:numel(Image(:,1,1))  
     for j=0:pxl-1  
       Image(i+j,:,:)=Image(i,:,:);  
     end  
   end  
   for i=1:pxl:numel(Image(1,:,1))  
     for j=0:pxl-1  
       Image(:,i+j,:)=Image(:,i,:);  
     end  
   end  
 end  
 end  
 function [txt]=im2txt()  
 [x,y]=size(ImageT);  
 txt=cell(x,y);  
 for i=1:70  
   if isempty(ImageT(ImageT==range(i)))==0  
   txt(ImageT==range(i))={[symbase(i),' ']};  
   end  
 end  
 fid=fopen([name,'.txt'], 'w');  
 for t=1:x  
   fprintf(fid,'%s\r\n',strcat([txt{t:x:end}]));  
 end  
 fclose(fid);  
 end  
 end  

Commentaires

Articles les plus consultés