Python + Photoshop

I posted a blog on how to automate Photoshop using Python, with some example scripts. Hope someone finds it useful.

http://techarttiki.blogspot.com/2008/08/photoshop-scripting-with-python.html

1 Like

Thanks Adam. I was thinking about this just last week! It’s like you’ve got some kind of mind reading ability.


import telepathy

I’m a fan of javascript so I never thought about doing this but it makes sense why one would. Thank you for sharing this Adam!

Where would we be w/o standardized interfaces…

…

Ahh, that’s cool! I’ve just started learning Python recently for Maya usage, but it’s very cool to see examples of it controlling Photoshop here, and Chris Evans demonstrated nicely how to control Motion Builder with it too, so it seems like the best language for me to learn right now :slight_smile:

1 Like

Finally had a chance to test it this evening. Man, it works fast. I can see this being exceptionally useful, especially if I can get it working for After Effects too. Thanks again Adam.

NP. You can do this with any program that registers a COM interface. I don’t have After Effects, but I imagine it provides that.

You can use any language that supports COM/OLE objects to do this. That’s one of the main features of COM, that it’s language-independent.

[QUOTE=Adam Pletcher;853]NP. You can do this with any program that registers a COM interface. I don’t have After Effects, but I imagine it provides that.

You can use any language that supports COM/OLE objects to do this. That’s one of the main features of COM, that it’s language-independent.[/QUOTE]

It does (After Effects has the same interface and scripting options as PS), but I can’t get the fundamental part working… ie. the client.Dispatch name!

Normally com dispatches are done via ‘X.Application’ where X = the app progID in question (such as ‘Excel.Application’ or ‘MAX.Application.2009’), but i can’t get AE (ie. ‘AfterFX.Application’) to register. I probably have the wrong name, but my ignorance is showing about identifying COM Objects.:D:

EDIT: Turns out i’m an idiot… they’re right there in PythonWin tools… And no, AE doesn’t have one! I may have to set up an automation entry.

Yet another killer example, Adam. Thanks a bunch for this; it was very enjoyable to pick apart:):.

[QUOTE=Adam Pletcher;846]I posted a blog on how to automate Photoshop using Python, with some example scripts. Hope someone finds it useful.

http://techarttiki.blogspot.com/2008/08/photoshop-scripting-with-python.html[/QUOTE]
Sweet! Thank you mucho for showing this. Been needing to automate Photoshop forever now. Will try at work tomorrow.

Excellent timing, I’m just about to start looking into this so it’ll be a great help. Thanks!

Hello.

I’m implementing photoshop script via python by reference to Adam’s site.
(Thank you for very useful information!)

But I encountered a problem about dealing with HDRI format (such as Tiff 32bit).
I attempted to apply Blur filter to Tiff32bit image by using applyBlur() function of ‘ArtLayer’.
But I can’t do it. Photoshop output an error. (you can’t use ‘applyBlur()’ currently.)

So I guess it seems that Photoshop script doesn’t support for filter function of HDRI.
I’m using PhotoshopCS2. So, when I apply the Blur Filter to HDRI manually, I can do that.

Have you ever implemnted photoshop script for dealing with HDRI?
This is the simple source which I mentioned above.
Could you give me helpful advice, please?


import win32com.client

psApp = win32com.client.Dispatch('Photoshop.Application')
doc = psApp.Open(r"C:/Temp/test.tif"))
for layer in doc.Layers:
    layer.applyBlur()
doc.Save()

Thanks

If you change “layer.applyBlur()” to “layer.ApplyBlur()” it works, at least for me. Like regular Python functions, com methods are case-sensitive.

[edit] Ah, right, like you said ApplyBlur doesn’t work on HDR images. I’d venture a guess that the Filters have to be specifically written to support HDR/32 bit color. If you go to the Filter/Blur menu, “Blur” is disabled but a few others are not. For instance, this one works:

layer.ApplyGaussianBlur(13)

I should add, I’m using CS3. Hope that helps.

This is great, Python is next on my list to learn. By the way is it possible to use photoshop functions without opening Photoshop? If I for example make an application which converts psd files to png and applies a Photoshop filter to them?

No, the act of applying a filter still requires Photoshop to be running. Even if Python is pulling the strings, the Photoshop app is open, doing the legwork.

I need to ask: Why the hell isn’t there a 2d app with a proper scripting environment?

Imagine PS with the ability to customize like maya or xsi. the things we could do!

> Adam
Thank you for your information. Like you said, I made a mistake the spell of com methods. After that, I could execute the script. Thank you!

But I have an another issue about Photoshop scripts.
I’m trying to set the Gamma value “2.2” to HDR Image via Photoshop script.(Menu:Image -> Adjustments -> Exposure -> Gamma )

I found the appropriate command is ‘adjustLevels’ method of ‘ArtLayer’ from “JavaScript Referece Guide”.
As well as the previous one, I wrote the following source code.


import win32com.client

psApp = win32com.client.Dispatch('Photoshop.Application')
doc = psApp.Open(r"C:/Temp/test.tif"))
for layer in doc.Layers:
    [B]layer.adjustLevels(0,255,2.2,0,255)[/B]
doc.Save()

However, Photoshop also output an error.

[B] File “C:\Python26\lib\site-packages\win32com\client\dynamic.py”, line 495, in getattr
raise pythoncom.com_error, details pywintypes.com_error:
(-2147352567, ‘Exception occurred.’, (0, u’Adobe Photoshop’, u’Illegal argument - argument 1

  • Required value is missing’, None, 0, -2147220262), None)[/B]

It seems that I made a mistake for the usage of the function. But I can’t figure out this error.
So I would like to know the useful informations for association ‘an operation on Photoshop’ with ‘Photoshop script command’!

Could you tell me other useful informations for that? (except JavaScriptReferenceGuide.pdf)

Thanks,

dragonboy, a few things came off a bit different from Adam’s blog. You defined doc as the psApp’s .Open function instead of the it looking at the Application.ActiveDocument. Also, I’ve seen no difference in choosing ArtLayers vs. Layers, but I switched it to that anyways… some of the syntax in that PDF describing them and their case sensitivity confuses me anyways :?:. Hope this helps!

You had an extra ‘)’ in the psApp.Open function code you pasted for some reason as well but I can see you weren’t getting that syntax error.

import win32com.client

psApp = win32com.client.Dispatch('Photoshop.Application')

psApp.Open(r"C:\Temp	est.tif")
doc = psApp.Application.ActiveDocument  
layer = doc.ArtLayers[0]                
layer.AdjustLevels(0, 255, 2.2, 0, 255)

doc.Save()  

Thanks Adam! So hot.

You mention that photoshop supports several different COM interfaces, but i haven’t had much luck finding documentation on them. Do you have any suggestions on where I should be digging?

I gave up the python approach this time…
I remembered that Photoshop provides a convenient plugin ‘ScriptingListener’.

So I decided to use this plugin to execute automatic operation on Photoshop.
But, as you know, ScriptingListener output JavaScript’s log.
So I couldn’t use Adam’s useful approach. That’s too bad :frowning:

[edit]
> Kovac
Thank you for your information. I changed the code as you adviced. But Photoshop output the same error…
So I decided to change the approach by using “ScriptingListener”.

Thanks anyway.