Hello,
I'm scripting Photoshop CS3 through COM, using Python. The COM api is exactly the same as for Visual Basic, so the code example below should be equally trivial to follow for anybody.
I've stripped down the problem I'm having with the attached Jpeg image to bare bones:
1) open the file
2) save a PNG 24 for web
-> I get COM error -2147417851 (The server threw an exception).
If instead I (for example) resample the image in between 1) and 2), then Photoshop is happy.
Notice that if I do exactly the same operations by hand (using the UI and my mouse) there is no problem whatsoever.
This particular image is one I stumbled upon, but for many others this very same code works with no problems.
What is going on?
Is it an internal bug, or am I missing something?
Apparently this image triggers some unexpected behaviour.
This is the Python code: http://rafb.net/p/buw3dk18.html (also cut & paste below)
You can try to comment the middle section to see the problem.
Thanks,
Stefano Masini
Pragma 2000
www.pragma2000.com
------------------------------------------------------------------------------
import os
import win32com.client
psDisplayAllDialogs = 0x1
psDisplayErrorDialogs = 0x2
psDisplayNoDialogs = 0x3
srcFile = r'C:\buggy-jpeg.jpg'
dstFile = r'C:\buggy-jpeg-web.png'
if os.path.exists(dstFile):
os.remove(dstFile)
comApp = win32com.client.Dispatch('Photoshop.Application')
comApp.DisplayDialogs = psDisplayNoDialogs
comDoc = comApp.Open(srcFile)
# ----------------------------------------------------------------
# Slightly change the image resolution, with Bicubic resampling
# ----------------------------------------------------------------
resolutionDpi = comDoc.Resolution
desc = win32com.client.Dispatch("Photoshop.ActionDescriptor")
desc.PutUnitDouble(comApp.CharIDToTypeID("Rslt"),
comApp.CharIDToTypeID("#Rsl"),
resolutionDpi+1)
desc.PutBoolean(comApp.StringIDToTypeID("scaleStyles"), True) # Scale Styles
desc.PutBoolean(comApp.CharIDToTypeID("CnsP"), True) # Constrain Proportions
desc.PutEnumerated(comApp.CharIDToTypeID("Intr"),
comApp.CharIDToTypeID("Intp"),
comApp.CharIDToTypeID("Bcbc")) # Bicubic
comApp.ExecuteAction(comApp.CharIDToTypeID("ImgS"), desc, psDisplayNoDialogs)
# ----------------------------------------------------------------
# Save for Web, PNG24
# ----------------------------------------------------------------
desc2 = win32com.client.Dispatch("Photoshop.ActionDescriptor")
actionDesc = win32com.client.Dispatch("Photoshop.ActionDescriptor")
actionDesc.PutEnumerated(comApp.CharIDToTypeID("Op "),
comApp.CharIDToTypeID("SWOp"),
comApp.CharIDToTypeID("OpSa"))
actionDesc.PutEnumerated(comApp.CharIDToTypeID("Fmt "),
comApp.CharIDToTypeID("IRFm"),
comApp.CharIDToTypeID("PN24"))
actionDesc.PutBoolean(comApp.CharIDToTypeID("Intr"), False)
actionDesc.PutBoolean(comApp.CharIDToTypeID("Trns"), True)
actionDesc.PutBoolean(comApp.CharIDToTypeID("Mtt "), False)
actionDesc.PutInteger(comApp.CharIDToTypeID("MttR"), 255)
actionDesc.PutInteger(comApp.CharIDToTypeID("MttG"), 255)
actionDesc.PutInteger(comApp.CharIDToTypeID("MttB"), 255)
actionDesc.PutBoolean(comApp.CharIDToTypeID("SHTM"), False)
actionDesc.PutBoolean(comApp.CharIDToTypeID("SImg"), True)
actionDesc.PutBoolean(comApp.CharIDToTypeID("SSSO"), False)
actionDesc.PutList(comApp.CharIDToTypeID("SSLt"),
win32com.client.Dispatch("Photoshop.ActionList"))
actionDesc.PutBoolean(comApp.CharIDToTypeID("DIDr"), False)
actionDesc.PutPath(comApp.CharIDToTypeID("In "), dstFile)
desc2.PutObject(comApp.CharIDToTypeID("Usng"),
comApp.StringIDToTypeID("SaveForWeb"),
actionDesc)
comApp.ExecuteAction(comApp.CharIDToTypeID("Expr"), desc2,
psDisplayNoDialogs)