BookmarkSubscribeRSS Feed
art297
Opal | Level 21

The following code, using Matlab, will convert a jpg file to ASCII Art.  Who can provide a similar or better solution using SAS?

function [ imgascii ] = asciipic2( img, gcf, filename )

%asciis=[' .,:;rsiS52X39hGAHBM#&@'];

%asciis=[' .,:;risS52X3AGhHB9M#&@'];

%asciis=[' .,:;risA523XGShHM#B9&@'];

asciis=[' .,:;irXAs253hMHGS#9B&@'];

%asciis=['`''..-,:"_!~/\;*|^<+7r?v=iJLlYc)T{(}tsIVCxF325]1[uU4nzAXjfoZSyPweaKEHkGOh0M$N9#dq6RmDW%bpQ8Bg@&'];

temp=imread(img,'jpg');

imwrite(temp,'img.bmp','bmp');

img=imread('img.bmp','bmp');

img = imadjust(img, [0 0.8], [0 0.7]);

img=rgb2gray(img);

[rows, cols]=size(img);

%img=abs(hex2dec(dec2hex(img))-255);

img = 255 - double(img);

%img=reshape(img, rows, cols);

%extrarows=mod(rows,13);

%extracols=mod(cols,8);

extrarows=mod(rows,7);

extracols=mod(cols,4);

img=img(1:(end-extrarows),1:(end-extracols));

[rows, cols] = size(img);

%denseboxes=zeros(rows/13,cols/8);

denseboxes=zeros(rows/7,cols/4);

for ii=1:7%13

    for oo=1:4%13

        pixels=img(ii:7:end,oo:4:end);% pixels=img(ii:13:end,oo:8:end);

        denseboxes = denseboxes + pixels;

    end

end

%gcf=2.1;

denseboxes = denseboxes - min(min(denseboxes));

denseboxes = denseboxes./max(max(denseboxes));

denseboxes = denseboxes.^gcf;

map=linspace(min(min(denseboxes)),max(max(denseboxes)),23);

%map=max(max(denseboxes))+min(min(denseboxes))-logspace(log10(min(min(denseboxes))),log10(max(max(denseboxes))),23);

%imgascii=zeros(rows/13,cols/8);

imgascii=zeros(rows/7,cols/4);

for ii=1:(rows/7)%13

    for jj=1:(cols/4)%8

        which_char=1;

        for kk=1:numel(map)

            if denseboxes(ii,jj)>=map(kk)

                which_char=which_char+1;

            end

        end

        if(which_char>23),which_char=23; end

        imgascii(ii,jj)=asciis(which_char);

    end

end

imgascii=char(uint8(imgascii));

fid = fopen(filename, 'w');

for ii=1:size(imgascii,1)

    fprintf(fid, '%s\r\n', imgascii(ii,:));

end

fclose(fid);

end

2 REPLIES 2
Ksharp
Super User

Arthur.T,

I think it is hard for SAS. There is not built-in function to process a photo.

FriedEgg
SAS Employee

JMP has built in image processing tools.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1837 views
  • 0 likes
  • 3 in conversation