xxxxxxxxxx
61
/**************************************************
https://www.youtube.com/watch?v=55iwMYv8tGI
translat the pixels of an image into text
finish with rendering video as text ASCII characters
Image source
https://pixabay.com/fr/photos/chien-jack-russel-terrier-terrier-7417233/
#2 is https://editor.p5js.org/Anne-Laure/sketches/KiccHHDAc
**************************************************/
const density = "Ñ@#W$9876543210?!abc;:+=-,._ ";
let img;
function preload() {
img = loadImage("dog640x427.jpg");
}
function setup() {
createCanvas(400, 600);
img.resize( width - 20, 0 ) ;
}
function draw() {
background(220);
image( img, 10, 30 + img.height ) ;
let w = (width - 20) / img.width ;
let h = 1 ;
img.loadPixels() ;
for( let i = 0; i < img.width; i++ ) {
for( let j = 0; j < img.height; j++ ) {
const pixelIndex = ( i + j * img.width ) * 4 ;
const r = img.pixels[ pixelIndex + 0 ] ;
const g = img.pixels[ pixelIndex + 1 ] ;
const b = img.pixels[ pixelIndex + 2 ] ;
noStroke() ;
let greyVal = (r+g+b) / 3 ;
// fill( r, g, b ) ;
fill( greyVal ) ;
if( (j*h < img.height + 1) && (i*w < img.width+ 1) ) {
rect( i*w + 10, j*h + 10, w, h ) ;
}
}
noLoop() ;
}
}