python 以编程方式编辑 Photoshop PSD 文本图层
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/850899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Editing Photoshop PSD text layers programmatically
提问by EvanK
I have a multi-layered PSD, with one specific layer being non-rasterized text. I'm trying to figure out a way I can, from a bash/perl/python/whatever-else program:
我有一个多层 PSD,其中一个特定层是非光栅化文本。我正在尝试从 bash/perl/python/whatever-else 程序中找出一种方法:
- load the PSD
- edit the text in said layer
- flatten all layers in the image
- save as a web-friendly format like PNG or JPG
- 加载 PSD
- 编辑所述图层中的文本
- 展平图像中的所有图层
- 保存为网页友好格式,如 PNG 或 JPG
I immediately thought of ImageMagick, but I don't think I can edit the text layer through IM. If I can accomplish the first two steps some other programmatic way, I can always use ImageMagick to perform the last two steps.
我立即想到了ImageMagick,但我认为我无法通过 IM 编辑文本层。如果我可以用其他编程方式完成前两个步骤,我总是可以使用 ImageMagick 来执行最后两个步骤。
After a couple of hours of googling and searching CPAN and PyPI, I still have found nothing promising. Does anyone have advice or ideas on the subject?
在谷歌搜索和搜索 CPAN 和 PyPI 几个小时后,我仍然没有发现任何有希望的东西。有没有人有关于这个主题的建议或想法?
采纳答案by wr.
If you don't like to use the officially supported AppleScript, JavaScript, or VBScript, then there is also the possibility to do it in Python. This is explained in the article Photoshop scripting with Python, which relies on Photoshop's COM interface.
如果您不喜欢使用官方支持的 AppleScript、JavaScript 或 VBScript,那么也可以使用 Python 来实现。使用 Python 编写 Photoshop 脚本一文对此进行了解释,该文章依赖于 Photoshop 的 COM 接口。
I have not tried it, so in case it does not work for you:
If your text is preserved after conversion to SVGthen you can simply replace it by whatever tool you like. Afterwards, convert it to PNG (eg. by inkscape --export-png=...
).
我还没有尝试过,以防万一它对您不起作用:如果您的文本在转换为 SVG后保留,那么您可以简单地用您喜欢的任何工具替换它。然后,将其转换为 PNG(例如 by inkscape --export-png=...
)。
回答by wr.
Have you considered opening and editing the image in The GIMP? It has very good PSD support, and can be scripted in several languages.
您是否考虑过在 The GIMP 中打开和编辑图像?它具有非常好的 PSD 支持,并且可以用多种语言编写脚本。
Which one you use depends in part on your platform, the Perl interface didn't work on Windows the last I knew. I believe Scheme is supported in all ports.
你使用哪一个部分取决于你的平台,Perl 接口在我知道的最后一次在 Windows 上不起作用。我相信所有端口都支持 Scheme。
回答by Nick Meharry
The only way I can think of to automate the changing of text inside of a PSD would be to use a regex based substitution.
我能想到的在 PSD 内自动更改文本的唯一方法是使用基于正则表达式的替换。
- Create a very simple picture in Photoshop, perhaps a white background and a text layer, with the text being a known length.
- Search the file for your text, and with a hex editor, search nearby for the length of the text (which may or may not be part of the file format).
- Try changing the text, first to a string of the same length, then to something shorter/longer.
- Open in Photoshop after each change to see if the file is corrupt.
- 在 Photoshop 中创建一个非常简单的图片,可能是一个白色背景和一个文本层,文本的长度是已知的。
- 在文件中搜索您的文本,并使用十六进制编辑器在附近搜索文本的长度(可能是也可能不是文件格式的一部分)。
- 尝试更改文本,首先更改为相同长度的字符串,然后更改为更短/更长的字符串。
- 每次更改后在 Photoshop 中打开以查看文件是否已损坏。
This method, if viable, will only work if the layer in question contains a known string, which can be substituted for your other value. Note that I have no idea whether this will work, as I don't have Photoshop on this computer to try this method out. Perhaps you can make it work?
如果可行,此方法仅在相关图层包含已知字符串时才有效,该字符串可以替换为您的其他值。请注意,我不知道这是否可行,因为我在这台计算机上没有 Photoshop 来尝试这种方法。也许你可以让它工作?
As for converting to png, I am at a loss. If the replacing script is in Python, you may be able to do it with the Python Imaging Library (PIL, which seems to support it), but otherwise you may just have to open Photoshop to do the conversion. Which means that it probably wouldn't be worth it to change the text pragmatically in the first place.
至于转换成png,我不知所措。如果替换脚本在 Python 中,您可以使用 Python Imaging Library(PIL,它似乎支持它)来完成,否则您可能只需要打开 Photoshop 来进行转换。这意味着首先以务实的方式更改文本可能不值得。
回答by Alexandr Ciornii
You can use Photoshop itself to do this with OLE. You will need to install Photoshop, of course. Win32::OLE in Perl or similar module in Python. See http://www.adobe.com/devnet/photoshop/pdfs/PhotoshopScriptingGuide.pdf
您可以使用 Photoshop 本身通过 OLE 来执行此操作。当然,您需要安装 Photoshop。Perl 中的 Win32::OLE 或 Python 中的类似模块。请参阅http://www.adobe.com/devnet/photoshop/pdfs/PhotoshopScriptingGuide.pdf
回答by kartsims
You can also try this using Node.js. I made a PSD command-line tool
您也可以使用 Node.js 尝试此操作。我做了一个PSD命令行工具
One-line command install (needs NodeJS/NPM installed)
单行命令安装(需要安装 NodeJS/NPM)
npm install -g psd-cli
npm install -g psd-cli
You can then use it by typing in your terminal
然后您可以通过在终端中输入来使用它
psd myfile.psd -t
psd myfile.psd -t
You can check out the code to use it from another node script or use it through your shell is from another Bash/Perl/whatever script.
您可以查看代码以从另一个节点脚本中使用它,或者通过来自另一个 Bash/Perl/任何脚本的 shell 使用它。
回答by AmbroseChapel
If you're going to automate Photoshop, you pretty much have to use Photoshop's own scripting systems. I don't think there's a way around that.
如果您要使 Photoshop 自动化,则几乎必须使用 Photoshop 自己的脚本系统。我认为没有办法解决这个问题。
Looking at the problem a different way, can you export from Photoshop to some other format which supports layers, like PNG, which is editable by ImageMagick?
以不同的方式看待这个问题,您能否从 Photoshop 导出到其他支持图层的格式,例如 ImageMagick 可编辑的 PNG?