自动化批处理脚本 - 在 Photoshop 中将文件名转换为文本

发布于 2024-10-09 14:29:17 字数 252 浏览 6 评论 0原文

如何在 Photoshop 中将一堆文件的每个文件名转换为文本图层(并保存)?

文件夹 1:数百个文件

文件夹 2:几乎相同的文件,但每个文件的文件名都贴在其图像上

这是一个破解的片段。我似乎无法解决的错误是如何将 activeDocument 设置为当前打开的文档。 http://pastebin.com/b0fqG9v4

How do you convert each filename of a bunch of files, each to a text layer (and save) in Photoshop?

Folder 1: Hundereds of files

Folder 2: Nearly the same files, but each with its filename plastered onto its image

Here is a hacked up snippet. The error that I can't seem to get by is how to set the activeDocument to the currently open one. http://pastebin.com/b0fqG9v4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-10-16 14:29:17

尚未被标记为已回答,因此这里有一个 CS 的 javascript 示例(也应该适用于更高版本),以防有人关心这样的事情。这将打开源文件夹中的每个图像文件并添加新的文本图层。将文件名设置为文本图层的内容,然后将新文件保存到输出位置。

它并没有真正尝试将文本适合图像,这当然是可能的,但很复杂。

如果您将颜色管理设置为在打开带有与工作色彩空间不匹配的嵌入配置文件的文件时发出警告,您将看到配置文件不匹配对话框。您可以设置您的首选项自动处理这种情况。

function main ()
{
    if (0 < app.documents.length)
    {
        alert ("Please close all open documents before running this script.");
        return;
    }

    // Use folder selection dialogs to get the location of the input files
    // and where to save the new output files.
    var sourceFolder = Folder.selectDialog ("Please choose the location of the source image files.", Folder.myDocuments);
    var destFolder = Folder.selectDialog ("Please choose a location where the new image files will be saved.", sourceFolder);

    var files = sourceFolder.getFiles();
    for (var i = 0; i < files.length; i++)
    {
        var f = files[i];
        if (f instanceof Folder)
            continue;

        // If there are no other documents open doc is the active document.
        var doc = app.open (f);
        var layer = doc.artLayers.add ();
        layer.bounds = [0,0,doc.width,doc.height];

        // Now make the layer into a text layer and set parameters.
        // The text will be centered, in the hideous default font, and white.
        // Note that font size depends not just on the point size, but also
        // on the resolution, which is NOT being set anywhere.
        layer.kind = LayerKind.TEXT;
        layer.textItem.position = [Math.round (doc.width / 2),Math.round (doc.height / 2)];
        layer.textItem.justification = Justification.CENTER;
        layer.textItem.color.rgb.hexValue = "FFFFFF";
        layer.textItem.size = 60;

        // Get the file name and set it as the text this assumes the full path is not wanted.
        // to set the full path swap fsname for name.
        layer.textItem.contents = File.decode (f.name);

        doc.flatten ();

        // Save as a new JPG file with these options.
        var options = new JPEGSaveOptions ();
        options.quality = 8;

        var outputFile = new File (destFolder.absoluteURI + "/" + f.name);
        doc.saveAs (outputFile, options, false, Extension.LOWERCASE);
        doc.close ();
    }
}

Hasn't been marked as answered so here's a javascript example for CS (and should work on later versions too) in case anyone cares for such a thing. This opens each image file in a source folder and adds a new text layer. The name of the file is set as the content of the text layer, then the new file is saved to an output location.

It does not really try to fit the text on the image, that's certainly possible but complicated.

If you have color management set to warn you when opening a file with an embedded profile that doesn't match the working color space you will get a profile mismatch dialog. You could set your preferences handle such a situation automatically.

function main ()
{
    if (0 < app.documents.length)
    {
        alert ("Please close all open documents before running this script.");
        return;
    }

    // Use folder selection dialogs to get the location of the input files
    // and where to save the new output files.
    var sourceFolder = Folder.selectDialog ("Please choose the location of the source image files.", Folder.myDocuments);
    var destFolder = Folder.selectDialog ("Please choose a location where the new image files will be saved.", sourceFolder);

    var files = sourceFolder.getFiles();
    for (var i = 0; i < files.length; i++)
    {
        var f = files[i];
        if (f instanceof Folder)
            continue;

        // If there are no other documents open doc is the active document.
        var doc = app.open (f);
        var layer = doc.artLayers.add ();
        layer.bounds = [0,0,doc.width,doc.height];

        // Now make the layer into a text layer and set parameters.
        // The text will be centered, in the hideous default font, and white.
        // Note that font size depends not just on the point size, but also
        // on the resolution, which is NOT being set anywhere.
        layer.kind = LayerKind.TEXT;
        layer.textItem.position = [Math.round (doc.width / 2),Math.round (doc.height / 2)];
        layer.textItem.justification = Justification.CENTER;
        layer.textItem.color.rgb.hexValue = "FFFFFF";
        layer.textItem.size = 60;

        // Get the file name and set it as the text this assumes the full path is not wanted.
        // to set the full path swap fsname for name.
        layer.textItem.contents = File.decode (f.name);

        doc.flatten ();

        // Save as a new JPG file with these options.
        var options = new JPEGSaveOptions ();
        options.quality = 8;

        var outputFile = new File (destFolder.absoluteURI + "/" + f.name);
        doc.saveAs (outputFile, options, false, Extension.LOWERCASE);
        doc.close ();
    }
}
囚你心 2024-10-16 14:29:17

显然,Photoshop 可以通过 Javascript 编写脚本,因此使用虚拟文件作为模型,您应该能够执行您需要的操作。以下是一些有关 Photoshop 脚本的资源:

Apparently, Photoshop is scriptable via Javascript, so with a dummy file as a model, you should be able to do what you need. Here are some resources about Photoshop scripting :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文