Home Technical Talk

Photoshop + Python problem

polycounter lvl 14
Offline / Send Message
haiddasalami polycounter lvl 14
Cross posting from tech-artists in hopes of getting something so I can start working on this :( Anywho, following Adam's blog post (http://techarttiki.blogspot.com/2008/08/photoshop-scripting-with-python.html) about COM and python and for some reason the code breaks at the open stage. Here's the code:
import win32com.client  
psApp = win32com.client.Dispatch("Photoshop.Application") 
psApp.Open(r"C:\test.jpg")       
doc = psApp.Application.ActiveDocument  
layer = doc.ArtLayers[0]               
layer.AdjustBrightnessContrast(20,-15)   
doc.Save()

pretty much a copy and paste and I get this as an error
Traceback (most recent call last):
File "C:\Users\Alistair Braz\workspace\Test\test.py", line 3, in <module>
psApp.Open(r"C:\test.jpg")
File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 505, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Adobe Photoshop', u'Illegal argument - argument 1\n- Required value is missing', None, 0, -2147220262), None)

Any ideas? I thought it was 2.7 been weird so moved to 2.6 and still nothing. Installed 2.6 pywin and getting same thing. Photoshop is 64bit and I installed the 64bit version of pywin too.

Replies

  • haiddasalami
    Options
    Offline / Send Message
    haiddasalami polycounter lvl 14
  • Ben Apuna
    Options
    Offline / Send Message
    Hmm...

    I just ran this:
    import win32com.client psApp = win32com.client.Dispatch("Photoshop.Application")
     psApp.Open(r"G:\test.psd")         # Opens a PSD file
     doc = psApp.Application.ActiveDocument  # Get active document object
     layer = doc.ArtLayers[0]                # Get the bottom-most layer
     layer.AdjustBrightnessContrast(20,-15)  # Bright +20, Contrast -15
     doc.Save()
    
    And it worked! Sweet! :)

    The psd already existed before running the script.

    I'm using Photoshop CS2, 32bit Python 2.6.6, and pywin32-216.win32-py2.6.exe from here.

    I ran the script from within my IDE (spyder).

    Good luck, and thanks for getting me off my ass to actually try this. I always wondered if it would work.

    EDIT:

    I just checked to make sure that script works on it's own without spyder, and it does, so that's not what's causing your problem.

    It looks like a read error to me, but I'm just guessing. Does the file you are trying to open exist already? and do you have read/write permissions on that drive?

    EDIT2:

    Just tested with a JPG, that worked too, so that's not it either.

    EDIT3:

    Yup tested with the file missing and got this:
    Traceback (most recent call last):
      File "G:\testPsOpen.py", line 10, in <module>
        psApp.Open(r"G:\test.jpg")         # Opens a PSD file
      File "<COMObject Photoshop.Application>", line 3, in Open
    pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Adobe Photoshop', u'Illegal argument - argument 1\n- Expected a reference to an existing File/Folder', None, 0, -2147220262), None)
    

    So that might possibly be your problem, maybe...

    The file definitely needs to exist before you run the script.

    Good luck :)

    Damn! now I don't want to sleep, PS + Python, Yay!
  • iconoplast
    Options
    Offline / Send Message
    iconoplast polycounter lvl 13
    You should also try using 32-bit python instead of the 64-bit -- you only need 64-bit python if you need access to the extra memory, and I don't think win32com plays nice with the win64 python build. (That is also the other difference between what you did and what Ben did.)
  • haiddasalami
    Options
    Offline / Send Message
    haiddasalami polycounter lvl 14
    Well Ill be damned. It was UAC. just got it open! Thanks so much Ben and icono :)

    @Ben: Python and Photoshop is gonna be so much win :)
Sign In or Register to comment.