Editing Photoshop PSD text layers programmatically

2019-02-01 02:37发布

问题:

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:

  1. load the PSD
  2. edit the text in said layer
  3. flatten all layers in the image
  4. save as a web-friendly format like PNG or 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.

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?

回答1:

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=...).



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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?



回答6:

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.