Editing Photoshop PSD text layers programmatically

僤鯓⒐⒋嵵緔 提交于 2019-11-28 18:27:26

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.

I have not tried it, so in case it does not work for you: If your text is preserved after conversion to SVG then you can simply replace it by whatever tool you like. Afterwards, convert it to PNG (eg. by inkscape --export-png=...).

Have you considered opening and editing the image in The GIMP? It has very good PSD support, and can be scripted in several languages.

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.

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.

  1. Create a very simple picture in Photoshop, perhaps a white background and a text layer, with the text being a known length.
  2. 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).
  3. Try changing the text, first to a string of the same length, then to something shorter/longer.
  4. Open in Photoshop after each change to see if the file is corrupt.

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?

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.

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

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.

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?

You can also try this using Node.js. I made a PSD command-line tool

One-line command install (needs NodeJS/NPM installed)

npm install -g psd-cli

You can then use it by typing in your terminal

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!