Python/Photosop

Hi guys,

I am creating a python script which batch processes textures. Identifies potential candidates to be converted to grey scale and mode and process them in photoshop.

I’m having trouble with the “changeMode” command. Can’t find the right syntax to do it.

This is what I found in Photoshop’s documentation:

Document.changeMode (destinationMode: ChangeMode , options: DocumentConversionOptions )

And this is my code:

import win32com.client  
psApp = win32com.client.Dispatch("Photoshop.Application")
psApp.Open("test.tga")
aDoc = psApp.Application.activeDocument
aDoc.changeMode("Grayscale")
psApp.close(1)

I have tried many different ways of passing the parameter but i always get the same error.

I’m getting this error:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Adobe Photoshop', u'Illegal argument - argument 1
- Required value is missing', None, 0, -2147220262), None)
File "d:\Projects\Python\_image\find_greyscale_images.py", line 29, in <module>
  aDoc.changeMode(ChangeMode)
File "C:\Python26\Lib\site-packages\win32com\client\dynamic.py", line 501, in __getattr__
  ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)

I guess I’m not passing the arguments correctly but I can’t figure out how to do it.

Anyone can help?

Cheers!
R.

Hi Swat3d,

I had a quick look at this and it seems you were pretty close, though the command should’ve been: “aDoc.ChangeMode” - note the captial “C”.

Judging from the Adobe VBScript pdf, you need to specify a pre-definied number to change the document mode, found on page 152: http://www.adobe.com/devnet/photoshop/pdfs/VisualBasicReferenceGuide.pdf

Constant Type:
PsChangeMode

Values:
1 (psConvertToGrayscale)
2 (psConvertToRGB)
3 (psConvertToCMYK)
4 (psConvertToLab)
5 (psConvertToBitmap)
6 (psConvertToIndexedColor)
7 (psConvertToMultiChannel)

So: “aDoc.ChangeMode(1)” converts the image to grayscale.

Hope this helps!

-w

Did that work for you? I can’t test it currently but if you still have problems try

psApp = win32com.client.dynamic.Dispatch(“Photoshop.Application”)

=> as far as I know the ‘dynamic’ makes all those constants like 1 for grayscale available

That code is for javascript, use the VB guide for reference. And VB is always capitalized:

import win32com.client  
psApp = win32com.client.Dispatch("Photoshop.Application")
psApp.Open("test.tga")
aDoc = psApp.Application.ActiveDocument
aDoc.ChangeMode("Grayscale")
psApp.Close(1)

And use the enums whw gave :slight_smile:

Great! It is working.
It was a “friday afternoon mistake” :stuck_out_tongue: “aDoc.changeMode(1)”, should be “aDoc.ChangeMode(1)”

Thanks very much for your quick replies and your great help guys!
Ruben Henares.