Scripting Photoshop with Python - Overwrite existing file?

I am having trouble overwriting an existing file using Python, comTypes and Photoshop.

The error message I am receiving is not very helpful.

Here is a little bit of code to illustrate


psApp.DisplayDialogs = 3 # display no dialogs
tgaOptions = comtypes.client.CreateObject('Photoshop.TargaSaveOptions')
tgaOptions.RLECompression = False
tgaOptions.Resolution = 32
tgaOptions.AlphaChannels = False

filepath = 'c:/temp/myTargaFile.tga'
doc.SaveAs(filepath, tgaOptions, True, 2) # filename, save options, save as copy, lowercase extension

The error message


_ctypes.COMError: (-2147212704, None, (None, None, None, 0, None))

So I think the problem is related to the fact that I am not opening the texture that is on disk. I am generating a new photoshop document from a layered PSD and then saving the new doc over the existing file.

existing layered PSD —> new photoshop document —> overwrite existing file on disk.

I tested this process manually by performing all the steps myself and didn’t have any problems saving over an existing file with a new photoshop document, but I can’t get it to work with scripting.

Are you sure that the directory exists? Also, have you tried it without the last arg?

doc.SaveAs(filepath, tgaOptions, True)

I am sure the file and folder exist and I have tried it both with and without the last argument, no luck.

I should note, that I am able to Save As Copy from a PSD to a TGA and overwrite the file… the problem is I am creating a new Document and trying to Save the New Document over the existing file. So I am very confused about what is going wrong.

EDIT: Solved. I use PILLOW to gather some metrics about the file if it exists and I was not properly releasing the file from memory before overwriting the file. Callimg Image.close() after gathering the data I needed fixed my problem.

can you try this in javasscript? Maybe use the debugger, it may give more or better info:


var doc = app.activeDocument;
app.displayDialogs = DialogModes.NO;
var tgaOptions = new TargaSaveOptions();
tgaOptions.rleCompression = false;
tgaOptions.resolution = TargaBitsPerPixels.THIRTYTWO;
tgaOptions.alphaChannels = false;

var f = new File('c:/tmp/test.tga');
doc.saveAs(f, tgaOptions, true);

this worked for me, btw