File: ef230-2023-08/www/examples/ascii_art.m Download
%! ASCII art example

% a small function to demonstrate the use of the matscii function
function main
clear all; close all; clc;
im=imread('http://ef.engr.utk.edu/c/pix/2008/bmarti15_th.jpg');
text(0,0,matscii(im,100),'fontname','courier new', ...
    'fontsize',8,'horizontalalign','left','verticalalign','bottom');
return

% MATSCII - Function to generate ASCII images
%
%  Usage: picci = matscii(im, width, gamma, filename)
%
%   im    - 2D array of image brightness colours.
%           Image can be grey scale or colour.
%   width - desired width of resulting character image.
%   gamma    - optional gamma value to enhance contrast,
%              gamma > 1 increases contrast, < 1 decreases contrast.
%   filename - optional filename in which to save the result.
%
%   picci - the resulting 2D array of characters.
%

% Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% pk @ csse uwa edu au
% http://www.csse.uwa.edu.au/~pk
%
% September 2000

function picci = matscii(im, width, gamma, filename)

% ASCII grey scale

%g = '$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`''. ';
g = '#8XOHLTI)i=+;:,. ';     % This seems to be a better `grey scale'
gmax = length(g);

charAspect = 0.55;            % Width/height aspect ratio of characters

if nargin <=2
  gamma = 1;                 % Default gamma value
end

im = double(im);

if ndims(im) == 3  % We have a colour image
  im = .3*im(:,:,1) + .59*im(:,:,2) + .11*im(:,:,2);   % Grey value = (R+G+B)/3
end

[rows, cols] = size(im);
scale = width/cols;

rows = round(charAspect * scale * rows);   % Rescaled rows and cols values
cols = round(scale * cols);

%im = imresize(im, [rows, cols]);
im = myrescale(im, [rows, cols]);    % Use this if you do not have the image toolbox

% Apply gamma and scale image values to range from 1 to gmax.

im = im - min(min(im));            % Offset image so that min value is now zero.
im = (im/max(max(im))).^gamma;     % Rescale so that max is 1 and apply gamma.
im = round(im*(gmax-1) + 1);       % Rescale to range 1..gmax and round to ints.

picci = char(zeros(rows,cols));    % Preallocate memory for output image.

for r = 1: rows
  for c = 1:cols
    picci(r,c) = g(im(r,c));
  end
end

if nargin == 4    % we have a filename
  [fid, msg] = fopen(filename,'wt');
  error(msg);
  for r = 1: rows
   fprintf(fid,'%s\n',picci(r,:));
  end
  fclose(fid);
end



%-------------------------------------------------------------------
% Internal function to rescale an image so that this code
% does not require the image processing toolbox to run.
%-------------------------------------------------------------------

function newim = myrescale(im,  newRowsCols)

[rows,cols] = size(im);
newrows = newRowsCols(1);
newcols = newRowsCols(2);

rowScale = (newrows-1)/(rows-1);     % Arrays start at 1 rather than 0
colScale = (newcols-1)/(cols-1);

newim = zeros(newrows, newcols);

% For each pixel in the final image find where that pixel `came from'
% in the source image - use this as the scaled image value
% Scaling eqns account for the fact that MATLAB arrays start at 1 rather than 0

for r = 1: newrows
  for c = 1: newcols

   sourceRow = round((r-1)/rowScale + 1);
   sourceCol = round((c-1)/colScale + 1);

   newim(r,c) = im(sourceRow, sourceCol);

  end
end