所以我基本上是在做一个图像,上面有一些语言字符,我想从图像中获取像素,以显示字符的图案,它们之间有一定距离,我写的代码可以正常工作,但是在那里有两个问题: 1.像素间距不一致 2.在粗体字符上正确分配像素
const PNGjs = require('pngjs-image');
const fs = require('fs');
let cordinates = [];
let newCordinates = [];
let minDistance = 20;
let count = 0;
let preX, preY = -1;
PNGjs.readImage('./assets/output/A.png', function(err, image) {
for(let i = 0; i < image.getHeight(); i++) {
for(let j = 0; j < image.getWidth(); j++) {
if(image.getPixel(j, i) === -16777216){
cordinates.push({x: j, y: i});
}
}
}
for(let i = 0; i < cordinates.length; i=i+80){
for(let j = 0; j < cordinates.length; j=j+50){
let cord1 = cordinates[i];
let cord2 = cordinates[j];
let min = parseInt(Math.sqrt(Math.pow(cord1.x - cord2.x, 2) + Math.pow(cord1.y - cord2.y, 2)).toFixed(0));
if(min > 20 && min < 40) {
newCordinates.push(cord2);
j = j + 50;
}
}
}
let p = [];
console.log('[STEP 3] : started')
for(let i = 0; i < newCordinates.length; i++){
if(p.indexOf(newCordinates[i]) === -1)
p.push(newCordinates[i])
}
// writes the file to coordinates.json
fs.writeFile('./coordinates.json', JSON.stringify(p), function(err) {
if (err) throw err;
console.log("coordinates.json saved");
})
});