【问题标题】:Photoshop layer x/y center registration point coordinatesPhotoshop 图层 x/y 中心配准点坐标
【发布时间】:2018-02-28 17:30:53
【问题描述】:

目标:

设计一个我可以在 Photoshop 中运行的脚本,它将为我提供 PSD 文件中每个图层的 x 和 y 坐标,然后将其保存到一个文本文件中,我可以使用它来提取所述数据。

进展:

我已经找到了一个可以完成此任务的脚本,并在其中添加了自己的内容。

问题:

我的问题是我不是一个非常高级的编码器,我试图获取图层中心注册点的 x 和 y 坐标,而不是左上角。我已经进行了数小时的研究(也许没有向谷歌询问正确的问题)来试图弄清楚这一点。我意识到我的理解水平与实际的软件开发人员不相上下,虽然我尊重这一点,但没有开发人员在这里工作,我只能独自解决这个问题。我学过一点javascript,所以我理解的足够多,可以对下面的代码做些小补充。

守则:

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var layerNum = app.activeDocument.artLayers.length;


// Define variable for the active layer in the active document
var layerRef = app.activeDocument.activeLayer;


// Define varibles for x and y of layers
var x = layerRef.bounds[0].value;
var y = layerRef.bounds[1].value;
var coords = "";

// Loop to iterate through all layers

function recurseLayers(currLayers) {
  for ( var i = 0; i < currLayers.layers.length; i++ ) {
    layerRef = currLayers.layers[i];
    x = layerRef.bounds[0].value;
    y = layerRef.bounds[1].value;
    coords += layerRef.name + ": " + x + "x" + "," + y + "y" + "\n";

//test if it's a layer set

    if ( isLayerSet(currLayers.layers[i]) ) {
      recurseLayers(currLayers.layers[i]);
    }
  }
}

//a test for a layer set

function isLayerSet(layer) {
  try {
    if ( layer.layers.length > 0 ) {
      return true;
    }
  }

  catch(err) {
    return false;
  }
}

// Ask the user for the folder to export to

var FPath = Folder.selectDialog("Save exported coordinates to");

// Detect line feed type

if ( $.os.search(/windows/i) !== -1 ) {
  fileLineFeed = "Windows";
}
else {
  fileLineFeed = "Macintosh";
}

// Export to txt file

function writeFile(info) {
  try {
    var f = new File(FPath + "/" + docName + ".txt");
    f.remove();
    f.open('a');
    f.lineFeed = fileLineFeed;
    f.write(info);
    f.close();
  }
  catch(e){}
}

// Run the functions

recurseLayers(docRef);
preferences.rulerUnits = defaultRulerUnits;

// Set preferences back to user's defaults

writeFile(coords);

// Show results

if ( FPath == null ) {
  alert("Export aborted", "Canceled");
}
else {
  alert("Exported " + layerNum + " layer's coordinates to " + FPath + "/" + 
docName + ".txt " + "using " + fileLineFeed + " line feeds.", "Success!");
}

【问题讨论】:

  • 我正在寻找的解决方案是以 x 和 y 坐标的形式找到图层边界框(或图像边界框)的中心,您通常必须通过复制和粘贴手动获得Photoshop 工具栏。

标签: javascript adobe photoshop


【解决方案1】:

图层的bounds 属性为您提供左上角。中间点是 x + 一半的层宽和 y + 一半的层高。您只需要图层尺寸和一些数学知识。

【讨论】:

  • 感谢您的帮助,我打算回到这个问题上。我能够让它与那种类型的信息一起工作。谢谢
猜你喜欢
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
相关资源
最近更新 更多