使用脚本替换 Photoshop 图层中的图像
Replace image in a layer in Photoshop using scripting
我提供了一个 JSON 对象作为我的脚本的输入,它基本上改变了 .psd
文件文本层的内容并为我保存了一个新的 .jpg
。现在我想添加一个功能来改变图像层中的图像。我应该怎么做才能实现该功能?
JSON 对象将具有 link 图像。 JSON 输入看起来像:
{
"appName": "abc",
"developerName": "XYZ Inc.",
"storeName": "Random Store",
"ctaText": "CTA Message",
"imageLink": "https://is4-ssl.mzstatic.com/image/thumb/Purple114/v4/9a/ba/d6/9abad636-8d6c-a9df-3910-607fe11f5ed6/source/100x100bb.jpg"
}
这是我当前的代码。请让我知道需要添加什么。
#include json2.js
var input = loadJSON('test.json');
var doc = app.activeDocument;
//Changing App Name in a Text Layer
var layer9 = doc.layerSets.getByName('Layer 9');
var appNameText = layer9.layers[1];
appNameText.textItem.contents = input.appName;
//Saving the template in JPEG Format
saveJpeg(input.appName);
//Changing Developer Name in a Text Layer
var developerNameText = layer9.layers[0];
developerNameText.textItem.contents = input.developerName;
//Changing Store Name in a Text Layer
var layer3 = doc.layerSets.getByName('Layer 3');
var storeNameText = layer3.layers[0];
storeNameText.textItem.contents = input.storeName;
//Changing CTA Text in a Text Layer
var layer4 = doc.layerSets.getByName('Layer 4');
var ctaText = layer4.layers[0];
ctaText.textItem.contents = input.ctaText;
//Load JSON
function loadJSON(relPath){
var script = new File($.fileName);
var jsonFile = new File(script.path + '/' + relPath);
jsonFile.open('r');
var str = jsonFile.read();
jsonFile.close();
return JSON.parse(str);
}
//Save JPEG
function saveJpeg(name){
var file = new File(app.activeDocument.path + '/' + name + '.jpg');
var opts = new JPEGSaveOptions();
opts.quality = 10; //High quality JPEG save
doc.saveAs(file, opts, true);
}
供您参考,logo 是我要替换为新图像的图层(来自 JSON 输入中提供的 link )
感谢您的帮助。非常感谢!
我附上了文件,以便您了解我要编辑的内容。
我想用新的徽标图片替换蓝色徽标框。
好的,第三次幸运:
你的psd文件:
与蓝色方块大小相同的新(粉红色)徽标
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF
// Call it the source document
var srcDoc = app.activeDocument;
// load image
var imageToAdd = "D:\temp\pink.png";
place_image_here(imageToAdd, srcDoc);
translate_layer(484, 632);
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF
// function PLACE IMAGE HERE (source image, destination image)
// --------------------------------------------------------
function place_image_here(fromimage, toimage)
{
var fileRef = new File(fromimage);
// If it's there, open it!
if (fileRef.exists)
{
app.open(fileRef);
// Establish the newly opened doc
// is the from document
var fromDoc = app.activeDocument;
var fromDocName = app.activeDocument.name;
// Get the name of the destination image
var toImageName = toimage.name;
// Establish the from and to documents
var to = app.documents.getByName(toImageName);
var from = app.documents.getByName(fromDocName);
// Select the tempImage
app.activeDocument = from;
// Move from tempImage to the baseImage
var duplicateLayer = activeDocument.activeLayer.duplicate(to);
// Close the temp image without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
else
{
alert("Error opening\n" + fromimage);
}
}
// function TRANSLATE LAYER(dx, dy)
// --------------------------------------------------------
function translate_layer(dx,dy)
{
// =======================================================
var id2014 = charIDToTypeID( "Trnf" );
var desc416 = new ActionDescriptor();
var id2015 = charIDToTypeID( "null" );
var ref287 = new ActionReference();
var id2016 = charIDToTypeID( "Lyr " );
var id2017 = charIDToTypeID( "Ordn" );
var id2018 = charIDToTypeID( "Trgt" );
ref287.putEnumerated( id2016, id2017, id2018 );
desc416.putReference( id2015, ref287 );
var id2019 = charIDToTypeID( "FTcs" );
var id2020 = charIDToTypeID( "QCSt" );
var id2021 = charIDToTypeID( "Qcsa" );
desc416.putEnumerated( id2019, id2020, id2021 );
var id2022 = charIDToTypeID( "Ofst" );
var desc417 = new ActionDescriptor();
var id2023 = charIDToTypeID( "Hrzn" );
var id2024 = charIDToTypeID( "#Pxl" );
desc417.putUnitDouble( id2023, id2024, dx );
var id2025 = charIDToTypeID( "Vrtc" );
var id2026 = charIDToTypeID( "#Pxl" );
desc417.putUnitDouble( id2025, id2026, dy );
var id2027 = charIDToTypeID( "Ofst" );
desc416.putObject( id2022, id2027, desc417 );
executeAction( id2014, desc416, DialogModes.NO );
}
我提供了一个 JSON 对象作为我的脚本的输入,它基本上改变了 .psd
文件文本层的内容并为我保存了一个新的 .jpg
。现在我想添加一个功能来改变图像层中的图像。我应该怎么做才能实现该功能?
JSON 对象将具有 link 图像。 JSON 输入看起来像:
{
"appName": "abc",
"developerName": "XYZ Inc.",
"storeName": "Random Store",
"ctaText": "CTA Message",
"imageLink": "https://is4-ssl.mzstatic.com/image/thumb/Purple114/v4/9a/ba/d6/9abad636-8d6c-a9df-3910-607fe11f5ed6/source/100x100bb.jpg"
}
这是我当前的代码。请让我知道需要添加什么。
#include json2.js
var input = loadJSON('test.json');
var doc = app.activeDocument;
//Changing App Name in a Text Layer
var layer9 = doc.layerSets.getByName('Layer 9');
var appNameText = layer9.layers[1];
appNameText.textItem.contents = input.appName;
//Saving the template in JPEG Format
saveJpeg(input.appName);
//Changing Developer Name in a Text Layer
var developerNameText = layer9.layers[0];
developerNameText.textItem.contents = input.developerName;
//Changing Store Name in a Text Layer
var layer3 = doc.layerSets.getByName('Layer 3');
var storeNameText = layer3.layers[0];
storeNameText.textItem.contents = input.storeName;
//Changing CTA Text in a Text Layer
var layer4 = doc.layerSets.getByName('Layer 4');
var ctaText = layer4.layers[0];
ctaText.textItem.contents = input.ctaText;
//Load JSON
function loadJSON(relPath){
var script = new File($.fileName);
var jsonFile = new File(script.path + '/' + relPath);
jsonFile.open('r');
var str = jsonFile.read();
jsonFile.close();
return JSON.parse(str);
}
//Save JPEG
function saveJpeg(name){
var file = new File(app.activeDocument.path + '/' + name + '.jpg');
var opts = new JPEGSaveOptions();
opts.quality = 10; //High quality JPEG save
doc.saveAs(file, opts, true);
}
供您参考,logo 是我要替换为新图像的图层(来自 JSON 输入中提供的 link )
感谢您的帮助。非常感谢!
我附上了文件,以便您了解我要编辑的内容。
我想用新的徽标图片替换蓝色徽标框。
好的,第三次幸运:
你的psd文件:
与蓝色方块大小相同的新(粉红色)徽标
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF
// Call it the source document
var srcDoc = app.activeDocument;
// load image
var imageToAdd = "D:\temp\pink.png";
place_image_here(imageToAdd, srcDoc);
translate_layer(484, 632);
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF
// function PLACE IMAGE HERE (source image, destination image)
// --------------------------------------------------------
function place_image_here(fromimage, toimage)
{
var fileRef = new File(fromimage);
// If it's there, open it!
if (fileRef.exists)
{
app.open(fileRef);
// Establish the newly opened doc
// is the from document
var fromDoc = app.activeDocument;
var fromDocName = app.activeDocument.name;
// Get the name of the destination image
var toImageName = toimage.name;
// Establish the from and to documents
var to = app.documents.getByName(toImageName);
var from = app.documents.getByName(fromDocName);
// Select the tempImage
app.activeDocument = from;
// Move from tempImage to the baseImage
var duplicateLayer = activeDocument.activeLayer.duplicate(to);
// Close the temp image without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
else
{
alert("Error opening\n" + fromimage);
}
}
// function TRANSLATE LAYER(dx, dy)
// --------------------------------------------------------
function translate_layer(dx,dy)
{
// =======================================================
var id2014 = charIDToTypeID( "Trnf" );
var desc416 = new ActionDescriptor();
var id2015 = charIDToTypeID( "null" );
var ref287 = new ActionReference();
var id2016 = charIDToTypeID( "Lyr " );
var id2017 = charIDToTypeID( "Ordn" );
var id2018 = charIDToTypeID( "Trgt" );
ref287.putEnumerated( id2016, id2017, id2018 );
desc416.putReference( id2015, ref287 );
var id2019 = charIDToTypeID( "FTcs" );
var id2020 = charIDToTypeID( "QCSt" );
var id2021 = charIDToTypeID( "Qcsa" );
desc416.putEnumerated( id2019, id2020, id2021 );
var id2022 = charIDToTypeID( "Ofst" );
var desc417 = new ActionDescriptor();
var id2023 = charIDToTypeID( "Hrzn" );
var id2024 = charIDToTypeID( "#Pxl" );
desc417.putUnitDouble( id2023, id2024, dx );
var id2025 = charIDToTypeID( "Vrtc" );
var id2026 = charIDToTypeID( "#Pxl" );
desc417.putUnitDouble( id2025, id2026, dy );
var id2027 = charIDToTypeID( "Ofst" );
desc416.putObject( id2022, id2027, desc417 );
executeAction( id2014, desc416, DialogModes.NO );
}