DATA Step, Macro, Functions and more

Keeping up FriedEgg's Puzzle Solution Contest tradition

Reply
PROC Star
Posts: 7,487

Keeping up FriedEgg's Puzzle Solution Contest tradition

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(1Smiley Sadend-extrarows),1Smiley Sadend-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=1Smiley Sadrows/7)%13

    for jj=1Smiley Sadcols/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,Smiley Happy);

end

fclose(fid);

end

Super User
Posts: 10,041

Re: Keeping up FriedEgg's Puzzle Solution Contest tradition

Arthur.T,

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

Trusted Advisor
Posts: 1,301

Re: Keeping up FriedEgg's Puzzle Solution Contest tradition

JMP has built in image processing tools.

Ask a Question
Discussion stats
  • 2 replies
  • 224 views
  • 0 likes
  • 3 in conversation