gpt4 book ai didi

javascript - Photoshop 脚本 - 未正确调整大小

转载 作者:行者123 更新时间:2023-12-03 18:52:31 25 4
gpt4 key购买 nike

我用 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

---更新---我已经设法弄清楚,尽管它被设置为像素,但按百分比变化。例如。文件应该设置为 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;

最佳答案

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

  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);

关于javascript - Photoshop 脚本 - 未正确调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com