Skip to content

Instantly share code, notes, and snippets.

Created April 1, 2014 23:08
Show Gist options
  • Save anonymous/9924932 to your computer and use it in GitHub Desktop.
Save anonymous/9924932 to your computer and use it in GitHub Desktop.
%% Read image and convert to grayscale
img = imread('pic.jpg');
img = rgb2gray(img);
%% Define grayscale representations of 0-255
chars = fliplr([' ', '.', ',', ':', '-', '=', '+', '*', '#', '%', '@']);
step = 256 / 11;
%% Cut image so its size is 8*w times 13*h
imgSize = size(img);
height = floor(imgSize(1) / 13);
width = floor(imgSize(2) / 8);
%% Create new "image" and translate the old one
ascii = char(zeros(height, width)) ;
for y = 1:height
for x = 1:width
% cut part of image
cut = img(1 + (y-1)*13 : y*13, 1 + (x-1)*8 : x*8);
% get mean
m = mean(mean(cut));
ascii(y, x) = chars(min(11, max(1, round(m / step))));
end
end
%% Write image
dlmwrite('out.txt',ascii,'delimiter','');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment