是否可以在JavaScript中读取图像的像素?

时间:2011-04-28 20:19:25

标签: javascript photoshop

我有一堆带有徽章的照片,接近1500张,我需要一种方法来检测它上面是否有黄色徽章。是否可以制作动作或脚本来每次从精确的像素坐标着色样本,如果它找到代表徽章的颜色然后将其发送到特定文件夹,则将其与发现的其他jpeg分组有徽章。对此有任何想法或意见会有帮助吗?

2 个答案:

答案 0 :(得分:3)

我知道这是旧的但是因为它在Photoshop中被标记(并且肯定可以在Photoshop中用JavaScript完成),所以这是一个解决方案:

#target photoshop

// TEST FUNCTION

function hasBadge(doc, x, y) {

    // remove all current color samplers because photoshop has a limit of 4 or so and create a new sampler at the coordinates
    for (var i=0; i<doc.colorSamplers.length; i++) {
        doc.colorSamplers[i].remove();
    }
    var sampler = doc.colorSamplers.add([x, y]);

    //This is where it could get tricky based on the actual color of the badge. If the badge is always consistently the same exact color you could test it's hexValue...
    if (sampler.color.rgb.hexValue === "ffff00") {
        return true;
    }

    // If the color is not consistent you can try to test if it's within a range of rgb values. This may take some tweaking...
    if (sampler.color.rgb.red > 200 && sampler.color.rgb.green > 200 && sampler.color.rgb.blue < 50) {
        return true;
    }

    return false;
}

// PROGRAM

var x = 200;
var y = 200;

// Process an entire folder. Can also use File.openDlg() to select files but might be easier to select by folder if you have a ton of files
var inputFolder = Folder.selectDialog("Select a folder to process");
var fileList = inputFolder.getFiles("*.JPG"); //Use whatever extension you want or no extension to select all files

// For each file in the folder...
for(var i=0; i<fileList.length; i++) {

    var doc = open(fileList[i]);

    if (hasBadge(doc, x, y) {
        doc.saveAs(new File("C:/my/file/path/" + doc.name));
        doc.close(SaveOptions.DONOTSAVECHANGES);
    }
}

答案 1 :(得分:0)

你可以在这里找到答案:

How to use JavaScript or jQuery to read a pixel of an image when user clicks it?

基本上你

  

在canvas元素中绘制图像   然后你可以使用getImageData   返回包含的数组的方法   RGBA值。