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

发布于 2024-11-04 14:41:24 字数 150 浏览 3 评论 0原文

我有一堆带有徽章的照片,接近 1500 张,我需要一种方法来检测上面是否有黄色徽章。是否可以执行一个操作或脚本,每次都从精确的像素坐标对样本进行颜色采样,如果它找到代表徽章的颜色,则将其发送到特定文件夹,以便将其与找到的其他 jpeg 分组在一起有徽章。对此有什么想法或意见会有帮助吗?

I have a bunch of photos that have badges on them, close to 1500 of them, I need a way to detect if there is a yellow badge on it. Is it possible to make a action or a script to color sample from a exact pixel coordinate every time and if it finds the color that represents the badge then send it to a particular folder, for it to be grouped with other jpegs that were found to have a badge. Any thoughts or opinions on this would be helpful?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-11-11 14:41:24

我知道这是旧的,但由于它是在 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);
    }
}

I know this is old but since it was tagged in Photoshop (and it definitely CAN be done in Photoshop with JavaScript), here is a solution:

#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);
    }
}
ㄖ落Θ余辉 2024-11-11 14:41:24

您可以在这里找到答案:

当用户点击图像时,如何使用 JavaScript 或 jQuery 读取图像的像素?

基本上你

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

You can find the answer here:

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

Basically you

draw the image in a canvas element
then you can use the getImageData
method to return an array containing
RGBA values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文