# Open a Photoshop document located in the Photoshop samples folder
# You must first create a File object to pass into the open method.
from comtypes.client import GetActiveObject

# Start up Photoshop application
# Or get Reference to already running Photoshop application instance
# app = Dispatch('Photoshop.Application')
app = GetActiveObject("Photoshop.Application")


fileName = "C:\Git\PS_Samples_Files\Layer Comps.psd"
docRef = app.Open(fileName)
Exemplo n.º 2
0
class WorkerThread(QThread):
    def __init__(self, parent=None):
        super(WorkerThread, self).__init__(parent)

        self.filestoprocess = []
        self.in_width = 0
        self.in_height = 0
        self.in_resample = 0
        self.in_resolution = 0

    def process_args(self, files, width, resolution, resample):
        self.filestoprocess = files
        self.in_width = int(width)
        self.in_resolution = resolution
        self.in_resample = resample

    def process_args2(self, files, width, height, resolution, resample):
        self.filestoprocess = files
        self.in_width = int(width)
        self.in_height = int(height)
        self.in_resolution = resolution
        self.in_resample = resample

    def run(self):
        try:
            self.psapp = GetActiveObject("Photoshop.Application")
            # We don't want any Photoshop dialogs displayed during automated execution
            psDisplayNoDialogs = 3  # from enum PsDialogModes
            self.psapp.displayDialogs = psDisplayNoDialogs

            psAutomatic = 8  # from enum PsResampleMethod
            psPreserveDetails = 9  # from enum PsResampleMethod
            psBicubicSmoother = 6  # from enum PsResampleMethod
            psBicubicSharper = 5  # from enum PsResampleMethod
            psBicubicAutomatic = 7  # from enum PsResampleMethod
            psNearestNeighbor = 2  # from enum PsResampleMethod
            psBilinear = 3  # from enum PsResampleMethod

            psBicubic = 4  # from enum PsResampleMethod
            psNoResampling = 1  # from enum PsResampleMethod

            for file in self.filestoprocess:
                print("thread: " + file)
                docRef = self.psapp.Open(file)

                # if height is given, don't maintain aspect ratio
                if int(self.in_height) > 0:
                    docRef.ResizeImage(self.in_width, self.in_height, None,
                                       psAutomatic)
                    # time.sleep(3)

                # maintain aspect ratio
                else:
                    doc_width = docRef.Width
                    doc_height = docRef.Height
                    # maintain aspect ratio
                    new_height = (doc_height / doc_width) * self.in_width
                    docRef.ResizeImage(self.in_width, new_height, None,
                                       psAutomatic)

                docRef.Save()
                docRef.Close()
                # to prevent application busy COM error
                time.sleep(1)
        except Exception as thread_err:
            print(thread_err)