xxxxxxxxxx
// original sketch https://www.openprocessing.org/sketch/533299
// https://goo.gl/sgDRu6
//
//
// (c) Tariq Rashid 2018
// sketch supports content of Make Your Own Algorithmic Art
// https://www.amazon.com/Make-Your-Own-Algorithmic-Art-ebook/dp/B07BP13VPR
//
//
// source image based on https://pixabay.com/en/salvador-guillermo-allende-gossens-155939/
// load portrait
var img;
function preload() {
img = loadImage('famous-portraits-4.jpg');
}
function setup() {
simple();
background('white');
// create array from image
img.loadPixels();
loop();
}
function draw() {
//strokeWeight(1);
noStroke();
for (var p = 0; p < 30; p += 1) {
var x = int(map(mouseX, width/2-img.width/2, width/2+img.width/2, 0, img.width));
var y = int(map(mouseY, height/2-img.height/2, height/2+img.height/2, 0, img.height));
x += randomNumber(-5, 5);
y += randomNumber(-5, 5);
// read pixel value
var [r, g, b] = get_rgb_from_image(img,x,y);
//paint
fill(r, g, b, 30);
var x1 = x + width/2 - img.width/2 + randomNumber(-5, 5);
var y1 = y + height/2 - img.height/2 + randomNumber(-5, 5);
circle(x1, y1, randomNumber(2, 12));
}
// border
noFill(); stroke(0); strokeWeight(1);
rect(width/2 - img.width/2 - 50, height/2 - img.height/2 - 50, img.width + 100, img.height + 100);
stroke(200); rect(0, 0, width-1, height-1);
}
// returns a greyscale value from image at given location
// retrun value is scaled between 0 and 1
function get_rgb_from_image(im, x, y) {
// red value
var r = im.pixels[4*x + (4*y*im.width)];
// green
var g = im.pixels[4*x + (4*y*im.width) + 1 ];
// blue
var b = im.pixels[4*x + (4*y*im.width) + 2 ];
// return array of 3 numbers
return [r, g, b];
}