【问题标题】:Photoshop Scripting - Not correctly resizingPhotoshop 脚本 - 未正确调整大小
【发布时间】:2018-06-23 05:12:13
【问题描述】:

我用 Javascript 为 photoshop 编写了一个脚本,它获取用户输入(桌面上的框数),然后将其转换为像素大小。问题在于调整大小,图层不会改变到那个量,它会改变那个量。例如。 microsoftbox 从 800*800 开始,然后应更改为 500*500,但将其添加到等于 1300*1300。

app.preferences.rulerUnits = Units.PIXELS;
//Get all input
var microsoftboxes = prompt("How many boxes for microsoft?");
var gamesboxesx = prompt("How many across boxes for games?");
var gamesboxesy = prompt("How many down boxes for games?");
var adobeboxes = prompt("How many boxes for adobe?");
var filesboxes = prompt("How many boxes for files?");
var toolsboxes = prompt("How many boxes for tools?");
var recycleboxes = 1;
//Add percentage on and convert to pixel num
var mb = microsoftboxes * 75;
mb = mb + (mb * 0.04);
var gbx = gamesboxesx * 75;
gbx = gbx + (gbx * 0.04);
var gby = gamesboxesy * 75;
gby = gby + (gby * 0.04);
var ab = adobeboxes * 75;
ab = ab + (ab * 0.04);
var fb = filesboxes * 75;
fb = fb + (fb * 0.04);
var tb = toolsboxes * 75;
tb = tb + (tb * 0.04);
var rb = recycleboxes * 75;
rb = rb + (rb * 0.04);
//vars for change size
var doc = app.activeDocument;
var m = doc.layers.getByName('microsoft');
var g = doc.layers.getByName('games');
var a = doc.layers.getByName('adobe');
var t = doc.layers.getByName('tools');
var f = doc.layers.getByName('files');
var r = doc.layers.getByName('recycle');
m.resize(mb, 73, AnchorPosition.MIDDLELEFT);
g.resize(gbx, gby, AnchorPosition.TOPCENTER);
a.resize(ab, 73, AnchorPosition.MIDDLELEFT);
f.resize(fb, 73, AnchorPosition.MIDDLELEFT);
t.resize(tb, 73, AnchorPosition.MIDDLELEFT);
r.resize(rb, 73, AnchorPosition.MIDDLELEFT);

Before After

---更新--- 我设法弄清楚它尽管被设置为像素,但按百分比变化。例如。 files 应该设置为 78,但它被设置为

250 (the current value) * 0.78(78 as a percentage) = 195 

文件被设置为 195 像素。有什么办法可以解决这个问题?

代码现在是:

var startru = app.preferences.rulerUnits;
var starttu = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
//Get all input
var microsoftboxes = prompt("How many boxes for microsoft?");
var gamesboxesx = prompt("How many across boxes for games?");
var gamesboxesy = prompt("How many down boxes for games?");
var adobeboxes = prompt("How many boxes for adobe?");
var filesboxes = prompt("How many boxes for files?");
var toolsboxes = prompt("How many boxes for tools?");
var recycleboxes = 1;
//Add percentage on and convert to pixel num
var mb = microsoftboxes * 75;
mb = mb + (mb * 0.04);
var gbx = gamesboxesx * 75;
gbx = gbx + (gbx * 0.04);
var gby = gamesboxesy * 75;
gby = gby + (gby * 0.04);
var ab = adobeboxes * 75;
ab = ab + (ab * 0.04);
var fb = filesboxes * 75;
fb = fb + (fb * 0.04);
var tb = toolsboxes * 75;
tb = tb + (tb * 0.04);
var rb = recycleboxes * 75;
rb = rb + (rb * 0.04);
//vars for change size
var doc = app.activeDocument;
var m = doc.layers.getByName('microsoft');
var g = doc.layers.getByName('games');
var a = doc.layers.getByName('adobe');
var t = doc.layers.getByName('tools');
var f = doc.layers.getByName('files');
var r = doc.layers.getByName('recycle');
m.resize(mb, 85, AnchorPosition.MIDDLELEFT);
g.resize(gbx, gby, AnchorPosition.TOPCENTER);
a.resize(ab, 85, AnchorPosition.MIDDLELEFT);
f.resize(fb, 85, AnchorPosition.MIDDLELEFT);
t.resize(tb, 85, AnchorPosition.MIDDLELEFT);
r.resize(rb, 85, AnchorPosition.MIDDLELEFT);
app.preferences.rulerUnits = startru;
app.preferences.typeUnits = starttu;

【问题讨论】:

  • 猜这太难了!
  • 我已经很长时间没有这样做了 - 但我很确定调整大小需要增量。所以尝试给它-300(例如,想从 800 中取 300 到 500)
  • 所以把我的代码改成m.resize(-mb, -73, AnchorPosition.MIDDLELEFT);我现在就试试。如果它可以作为答案写出来,我会接受。
  • 抱歉没用。我已经更新了帖子。
  • 我可以将原始值用作 100,因此百分比将等于原始值,但我正在寻找我的代码的问题。除非有人先这样做,否则我会尝试添加作为答案。

标签: javascript scripting photoshop photoshop-script


【解决方案1】:

正如我在评论中提到的,ArtLayer 对象的 resize 方法明确期望百分比值,并且不能接受调整大小的像素值。我确信有一个公式可以使用当前图层的大小来确定达到新的特定大小所需的百分比,但我认为阻力最小的路径是

  1. 将图层复制到新文档中
  2. 将其调整为您需要的确切像素尺寸
  3. 将该图层复制回原始文档

下面是我为实现这一目标而编写的示例。它不是针对您的问题的复制/粘贴解决方案,而是使用您想要做什么的想法的插图。我认为修改此代码以满足您的确切需求应该相当容易。

app.preferences.rulerUnits = Units.PIXELS;
var activeDoc = app.activeDocument;
//store the active document's resolution for when we create the temp doc
var res = activeDoc.resolution;

//capture the existing color mode for the temp doc
var newDocMode;
switch(activeDoc.mode) {
    case DocumentMode.RGB:
        newDocMode = NewDocumentMode.RGB;
        break;
    case DocumentMode.CMYK:
        newDocMode = NewDocumentMode.CMYK;
        break;
}

//my preference to store sizes as a JSON object
var msboxSizes = {
    sm: {
        width: 200,
        height: 200
    },
    md: {
        width: 500,
        height: 500
    }, 
    lg: {
        width: 800,
        height: 800
    }
};

//target our layer
var msboxLayer = activeDoc.artLayers.getByName("microsoftbox");

//make it the active layer
activeDoc.activeLayer = msboxLayer;

//get the bounds of the layer so we can create a new doc the same size as the image on this layer
var msboxSize = activeDoc.activeLayer.bounds;

//use the bounds to determine the width and height of the layer
var msboxWidth = (msboxSize[2].value - msboxSize[0].value).toFixed(0);
var msboxHeight = (msboxSize[3].value - msboxSize[1].value).toFixed(0);

//convert those values back into pixels
msboxWidth = new UnitValue(msboxWidth, "px");
msboxHeight = new UnitValue(msboxHeight, "px");

//copy our layer to the clipboard
msboxLayer.copy();

//create a new doc that is the same size, resolution and color mode as the layer we want to resize
var tempDoc = app.documents.add(msboxWidth, msboxHeight, res, "Temp Doc", newDocMode, DocumentFill.TRANSPARENT);

//set the temp doc as the active document
app.activeDocument = tempDoc;
//paste the layer into the temp doc
tempDoc.paste();

//store the different size obj keys in an array (could use array of objects and skip this bit)
var resizes = ["lg","md","sm"]
//loop through resizing to each size in the array, copy and paste them as layers into the original document
for (i = 0; i < resizes.length; i++) {
    var newWidth = msboxSizes[resizes[i]].width;
    var newHeight = msboxSizes[resizes[i]].height;

    app.activeDocument = tempDoc;
    tempDoc.resizeImage(newWidth, newHeight, res, ResampleMethod.BICUBIC);
    tempDoc.activeLayer.copy();

    app.activeDocument = activeDoc;
    activeDoc.paste();
}

//close the temp doc and dont save it
tempDoc.close(SaveOptions.DONOTSAVECHANGES);

【讨论】:

    猜你喜欢
    • 2015-02-24
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多