在 Photoshop 脚本中获取一个像素的颜色

lph*_*phd 5 javascript cs4 photoshop-script

我想弄清楚如何获得一个定义的像素的颜色。

在我的想象中,它应该是这样的:

color = get.color.Pixel(x,y);
Run Code Online (Sandbox Code Playgroud)

也许有人可以帮助我处理这段代码?

Rob*_*obC 8

Photoshop 的 JavaScript API 没有提供您在问题中想象的机制。

您需要使用该Document.colorSamplers.add([x, y])方法,然后通过其属性读取每个组件的颜色值:

以下要点示出了如何获得任一rgbcmyk对于给定的值x,y的坐标:

#target photoshop

// Define the x and y coordinates for the pixel to sample.
var x = 1;
var y = 1;

// Add a Color Sampler at a given x and y coordinate in the image.
var pointSample = app.activeDocument.colorSamplers.add([(x - 1),(y - 1)]);

// Obtain array of RGB values.
var rgb = [
    pointSample.color.rgb.red,
    pointSample.color.rgb.green,
    pointSample.color.rgb.blue
];

// Obtain array of rounded CMYK values.
var cmyk = [
    Math.round(pointSample.color.cmyk.cyan),
    Math.round(pointSample.color.cmyk.magenta), 
    Math.round(pointSample.color.cmyk.yellow),
    Math.round(pointSample.color.cmyk.black)
];

// Remove the Color Sampler.
pointSample.remove();

// Display the complete RGB values and each component color.
alert('RGB: ' + rgb)
alert('red: ' + rgb[0])
alert('green: ' + rgb[1])
alert('blue: ' + rgb[2])

// Display the complete CMYK values and each component color.
alert('CMYK: ' + cmyk)
alert('cyan: ' + cmyk[0])
alert('magenta: ' + cmyk[1])
alert('yellow: ' + cmyk[2])
alert('black: ' + cmyk[3])
Run Code Online (Sandbox Code Playgroud)

  • @darda 我刚刚测量了 100x100 像素区域的时间。重新创建颜色采样器时,需要 60 秒才能完成循环。移动颜色采样器时,需要 32 秒。快了一倍,但仍然慢得灾难性。如果移动颜色采样器,12兆像素的图片将在10小时内处理完毕。PS:8核Intel i7 (2认同)