Hello there,
Basically I want to load some images (in the folder pics/n*.jpg) and get their pixel data in a 2d array (data = []
).
I have this code :
let data = [];
let img;
function preload() {
for (let i = 0; i < 5; i++) {
img = loadImage('pics/n' + (i + 1) + '.jpg');
console.log(img);
let xs = [];
img.loadPixels();
for (let j = 0; j < img.pixels.length; j += 4) {
xs.push(img.pixels[j]);
}
data.push(xs);
}
console.log(data);
}
function setup() {
createCanvas(500, 500);
}
function draw() {
noLoop();
}
When I console.log the images I loaded with loadImage()
, their width and height are 1 but their propreties width and height are the right ones (172*254).
Can somebody explain this to me ? :
UPDATE :
More generally when I try to load an image when I concatenate strings and numbers, there’s the same error :
function setup() {
createCanvas(500, 500);
let img = loadImage('pics/n'+5+'.jpg');
console.log(img);
}
But it works when I use a callback function :
function setup(){
createCanvas(500,500);
let img = loadImage('pics/n'+5+'.jpg', () => {
console.log(img);
image(img,0,0);
});
}