Python Forum
Python Script to repeat Photoshop action in folders and subfolders
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Script to repeat Photoshop action in folders and subfolders
#1
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
Reply
#2
PNG8 means the color depth in bits for each channel.
JPG8, the same.

JPG should have additional optionns like quality.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What script to paste folders thenewcoder 1 676 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,267 Jun-29-2023, 11:57 AM
Last Post: gologica
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,354 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Pixel color and action Sartre 4 2,100 Apr-13-2023, 03:26 AM
Last Post: Sartre
Question Running an action only between certain times alexbca 9 1,732 Mar-15-2023, 04:21 PM
Last Post: deanhystad
  Checkbox itens with a button to run action Woogmoog 3 950 Dec-19-2022, 11:54 AM
Last Post: Woogmoog
  python move folders and subfolders not working mg24 5 2,185 Nov-09-2022, 02:24 PM
Last Post: Larz60+
Question Running an action only if time condition is met alexbca 5 1,317 Oct-27-2022, 02:15 PM
Last Post: alexbca
  Repeat request by else stsxbel 2 1,182 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  get out of while loop and stop repeat Frankduc 11 2,992 Apr-26-2022, 10:09 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020