Editing Photoshop PSD text layers programmatically - python

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:
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
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?

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

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

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.

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.

Related

I am looking for a python package which can apply 'resize page to drawing' to a svg files similar to Inkscape

I have a script which uses rdkit to create svg drawings of chemical molecules.
I will use them on a web server, therefore, the size of the figures does matter.
The current output is a string containing the svg file.
I know that I can use Inkscape to remove the white background and "resize page to drawing" which is exactly what I want. But as I am creating quiet a lot of figures I am looking for a way to resize the figures automatically.
My experience with python modules for svg is very limited, therefore I am hoping someone can suggest a module which can apply the needed changes.
I know that I can simply remove the
<rect style='opacity:1.0;fill:#ffffff;stroke:none' width='2000' height='2000' x='0' y='0'> </rect>
part from the svg file to remove the white background, but I have no Idea how to implement the 'resize page to drawing' part.
Any help is highly appreciated.
Inkscape can perform various functions from the command line, including what you are asking for.
My search first led me here:
That is an extensive list of many 'verbs' or actions that Inkscape can do from the command line. There are some linked pages from there but realized that information could be found with Inkscape's command line help
From the command line inkscape --help is very thorough.
The output from the link provided above is just a copy of this command:
inkscape --verb-list.
From that list, what we are looking for is FitCanvasToDrawing
Final Code
inkscape --verb=FitCanvasToDrawing --verb=FileSave --verb=FileClose --verb=FileQuit input.svg
The format for 'verbs' is --verb=VerbName and many different actions can be chained. After all verbs, you provide the filename that Inkscape is going to open/manipulate (unfortunately verbs CANNOT take arguments which would expand the scripting possibilities).
The last verb (FileQuit) in the example above is optional as if you are looping through many files doing this, it will probably be quicker to leave Inkscape open rather than restarting it every couple of seconds.
This last point brings up one of Inkscape's limitations and that is that the UI needs to be open to perform this action which might slow down this process for bigger batches but trivial for one-offs. The bug report for this can be found here and has been marked as a 'Wishlist' item.
Finally, you mention python, but this could be batched out with a shell script by providing a variable at the end instead of a static filename and running that script from the command line on all svg files in a folder like so script *.svg but feel free to use the language you are most comfortable with.

Writing PSD files with python

I have a PSD file with 3 layers. What I want to do is change the 3rd, deepest layer's image and save the whole PSD file as PNG.
Is this possible with python or the ubuntu command line?
I've already looked at psd tools but it's only good for exporting images as PNG in my case, I've looked at this blog post but not sure if it will allow me to change layer's image and where to find some documentation
If you really need Python, try to use photoshop-python-api
But even this not support all the functionalities of Photoshop.
Win32com API can give a vast range of functionalities but it's for windows.
Have you ever tried ExtendScript JS for Phothoshop? This is a built-in script editor for Adobe and a script listener is also available so you can easily make functions you need.
Scripting Guide
Scripting Listener

How to programmatically add a background to a pdf?

Given a .pdf file (produced by Inkscape with the LaTeX option, but I am more focused on the .pdf), is there a way to add programmatically a background (ideally, the background would be a rounded rectangle) to it? I am thinking typically of a python script or a C(or objective-C) library.
Typically:
I guess I should be easier to act directly (maybe via the command line) within Inkscape, but I am looking for a solution on the .pdf
If your background is also a PDF, you can use cpdf:
cpdf -stamp-under stamp.pdf input.pdf -o out.pdf
Working further from the answer of johnwhitington I found, that qpdf allows for that as well with the "underlay" option:
qpdf --underlay "background.pdf" -- input.pdf output.pdf
Great advantage: qpdf is available in most distributions. And as allways you can look at qpdf --help to find even more options to specify the page or to work inplace i.e. no need to specify an output file.
Ghostscript and Imagemagick allow for the same, but I found it quite hard to have a proper solution and the .pdf will become a rasterized image.

Templatizing images using gimp

My wife recently started a business making soap bars and the soap labels have quickly spiraled out of control into tons of diverging Gimp .xcf files. The only difference between the files are the names of the product, description, and ingredients. I'd like to make a template and produce the labels from a .xcf file and maybe a .xml or .ini file that has the text data. I've been eyeing python-fu, but I'm not quite sure if it does what I'd like. I'd like to stick to python where possible because it's simple.
Are there facilities in python + gimp to do what I'd like? Could something similar be done in anything other than gimp?
Edit: Additionally, I have to make pages of stickers to print, so some means of optimizing the number of stickers that can fit on a page (2d bin pack?) would be a big plus.
The Python Imaging Library (PIL) can quite easily read in an image, put some text on it, and write it back out. Use the Image module to read and write, and the ImageDraw module to add the text.
I doubt that it can use the .xcf format though, you'll probably want to convert to .png.
You can script GIMP in Python, and pretty much everything you can do o n the prgoram can be done via the API -- you can check for the available API functions in help->procedure browser.
To enable Python scripting in gimp 2.6 under Windows, you have to google for it -- Python, python gtk and one other package have to be installed before GIMP.
Python's PIL is fine for simple images, but it is weak in arttistic pixel manipulation, which is available with GIMP.

Python Audio Edit

I am searching for a way to write a simple python
program to perform an automatic edit on an audio file.
I wrote with PIL automatic picture resizing to a predefined size.
I would like to write the same for automatic file re-encoding into a predefined bitrate.
similarly, i would like to write a python program that can stretch an audio file and re-encode it.
do i have to parse MP3's by myself, or is there a library that can be used for this?
Rather than doing this natively in Python, I strongly recommend leaving the heavy lifting up to FFMPEG, by executing it from your script.
It can chop, encode, and decode just about anything you throw at it. You can find a list of common parameters here: http://howto-pages.org/ffmpeg/
This way, you can leave your Python program to figure out the logic of what you want to cut and where, and not spend a decade writing code to deal with all of the audio formats available.
If you don't like the idea of directly executing it, there is also a Python wrapper available for FFMPEG.
There is pydub. It's an easy to use library.

Categories

Resources