Python Forum

Full Version: Python Script to repeat Photoshop action in folders and subfolders
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

I am using Windows 7, Python 3.6, editor Pycharm.

My goal :
Apply recorded action in Photoshop to many folders and subfolders containing images

My inspiration :
https://vsfxryan.com/portfolio/python-ph...n-scripts/, although I would not like to use function definitions in my code, because I do not know how to do it.

My code :
import os
import shutil
import win32com.client

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

input_1 = "location input 1"
output = "location output"

for src_dir, dirs, files in os.walk(input_1):
    dst_dir = src_dir.replace(input_1, output, 1)
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)
    for file_ in files:
        src_file = os.path.join(src_dir, file_)
        dst_file = os.path.join(dst_dir, file_)
        if os.path.exists(dst_file):
            os.remove(dst_file)
        shutil.copy(src_file, dst_dir)

for dirpath, dirnames, filenames in os.walk(output):
            for filename in filenames:
                if filename.endswith(('.tif')):
                    doc = psApp.Open(output + "\\" + filename)
                    # Action 'jpg q8' in Photoshop makes some changes like converting to profile srgb :
                    doc = psApp.DoAction('jpg_q8','Default Actions')
                    # here I would like to save my file as jpg quality=8, but I do not know how to do it :    
                    doc.Export ?????
In https://vsfxryan.com/portfolio/python-ph...n-scripts/, there are lines and codes about saving the file as PNG8 (search 'PNG8' in the web page), but how to do it as JPG quality 8?

Thanks Smile
PNG8 means the color depth in bits for each channel.
JPG8, the same.

JPG should have additional optionns like quality.
Thanks DeaD_EyE.

Here is the code in the cited Web page :

 def edit_File( actionScript, imagePath, saveDir, psApp ):
    #png save options
    options = win32com.client.Dispatch('Photoshop.ExportOptionsSaveForWeb')
    options.Format = 13   # PNG
    options.PNG8 = True  # Sets it to PNG-8 bit

    #new folder save location
    folder, filename = os.path.split( imagePath )
    filename, ext = os.path.splitext( filename )
    filenamePng = filename + ".png"
    newSave = os.path.join( saveDir, filenamePng )

    ##&& check to make sure file does not already exist

    
    #open image
    doc = psApp.Open(imagePath, None)
    #run action script
    psApp.DoAction( actionScript, 'WaterMark')
    #save
    doc.Export(ExportIn=newSave, ExportAs=2, Options=options)
    return doc
I am wondering where I could find the value of options.Format in...
options.Format = 13   # PNG
so that I can get JPG instead of PNG... plus the additional options you are evoking...

Any idea?