需求描述:
在PS中用脚本实现将不同的二维码和文字批量处理到相同的一张图片上
结果呈现:
实现步骤:
请注意,这个过程假设所有的二维码都是同样的大小,而且你想要将它们放在同样的位置。如果你需要将二维码放在不同的位置,或者如果你的二维码有不同的大小,那么你可能需要创建多个动作,或者手动调整每个二维码。
在Adobe Photoshop中使用JavaScript脚本的步骤如下:
-
创建脚本:首先,你需要使用任何文本编辑器(如Notepad,Sublime Text,VS Code等)创建一个JavaScript文件。在这个文件中,你可以写入你的Photoshop脚本。保存这个文件时,你需要使用
.js
作为文件扩展名。当然这里你可以直接使用我的脚本,只需更换一下相应文件的路径。 -
打开Photoshop:启动Adobe Photoshop程序。
-
运行脚本:在Photoshop中,点击左上角的 "文件(File)" 菜单,然后选择"脚本(Scripts)" ,点击下方的 "浏览(Browse)" 。在弹出的文件选择对话框中,找到并选择你的
.js
脚本文件,然后点击Open
按钮。这将会运行你的脚本。
相关的文件夹:
脚本代码:
var sourceFolder = Folder.selectDialog("Select the folder with your QR codes");
var files = sourceFolder.getFiles("*.png"); // Assumes the QR codes are PNG files
// Assume the texts are stored in a text file, one text per line
var textFile = File('D:/你的文件夹名称/99 Logo/Microsoft.csv'); // Replace with the path to your text file
textFile.open('r');
var texts = [];
while (!textFile.eof) {
texts.push(textFile.readln().split(',')); // Split each line into name and phone number
}
textFile.close();
var doc = app.open(File('D:/你的文件夹名称/99 Logo/picture01.png')); // Replace with the path to your image
var targetX = 200; // Replace with the x-coordinate of the target position
var targetY = 450; // Replace with the y-coordinate of the target position
var textLayer; // Declare textLayer variable here
for (var i = 0; i < files.length; i++) {
// Reset the textLayer to undefined at the start of each iteration
textLayer = undefined;
var qr = app.open(files[i]);
app.activeDocument.selection.selectAll();
app.activeDocument.selection.copy();
qr.close(SaveOptions.DONOTSAVECHANGES); // Do not save changes
doc.paste();
doc.activeLayer.resize(30, 30, AnchorPosition.MIDDLECENTER); // Resize the QR code to 100px by 100px
//doc.activeLayer.resize(20, 20, AnchorPosition.MIDDLECENTER); // Adjust as needed to resize the QR code
doc.activeLayer.translate(100, 100); // Adjust as needed to position the QR code
var dx = targetX - doc.activeLayer.bounds[0].as('px');
var dy = targetY - doc.activeLayer.bounds[1].as('px');
doc.activeLayer.translate(dx, dy);
if (textLayer !== undefined) {
textLayer.remove(); // Remove the text layer from the previous iteration
}
// Create a new text layer
textLayer = doc.artLayers.add();
textLayer.kind = LayerKind.TEXT;
// Set the text content, if it exists, otherwise set a default value
if (texts[i] !== undefined) {
textLayer.textItem.contents = texts[i][0] + "\r" + texts[i][1]; // Set the text to name and phone number
} else {
textLayer.textItem.contents = "Default name\nDefault phone number"; // Set a default text
}
// Set the font and size of the text
textLayer.textItem.font = "Arial"; // Set the font to Arial
textLayer.textItem.size = 5; // Set the size to 12
// Set the position of the text layer
textLayer.textItem.position = [40, 500]; // Replace with the target position for the text
// Create a new solid color and set its RGB values
var myColor = new SolidColor();
myColor.rgb.red = 255;
myColor.rgb.green = 255;
myColor.rgb.blue = 255;
// Set the color of the text
textLayer.textItem.color = myColor;
var saveFile = File('D:/你的文件夹名称/99 Logo/2024年2月26日' + '/image' + i + '.png'); // Replace with the path to your save folder
var saveOptions = new PNGSaveOptions(); // Assumes you want to save as PNG
doc.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
doc.activeLayer.remove(); // Remove the QR code layer for the next iteration
}
doc.close(SaveOptions.DONOTSAVECHANGES);