photoshop-python-api

repository·main·Indexed 21 days ago

https://github.com/loonghao/photoshop-python-api

A Python API for Adobe Photoshop on Windows that uses COM (Component Object Model) to automate tasks such as creating documents, adding layers, manipulating colors, and saving files. Version 0.24.2.

Tokens
1K
Snippets
4
Records
4
Agent score
24%

What's inside photoshop-python-api

  1. Get started with the Photoshop Python API (Hello World)

    main

    To use the API, import photoshop.api and instantiate ps.Application(). This provides access to the Photoshop application object, which allows you to create documents, add layers, manipulate colors, and save files. Note that this library uses COM (Component Object Model) to connect to Photoshop and is only supported on the Windows platform.

    import photoshop.api as ps
    app = ps.Application()
    doc = app.documents.add()
    new_doc = doc.artLayers.add()
    text_color = ps.SolidColor()
    text_color.rgb.red = 0
    text_color.rgb.green = 255
    text_color.rgb.blue = 0
    new_text_layer = new_doc
    new_text_layer.kind = ps.LayerKind.TextLayer
    new_text_layer.textItem.contents = 'Hello, World!'
    new_text_layer.textItem.position = [160, 167]
    new_text_layer.textItem.size = 40
    new_text_layer.textItem.color = text_color
    options = ps.JPEGSaveOptions(quality=5)
    # # save to jpg
    jpg = 'd:/hello_world.jpg'
    doc.saveAs(jpg, options, asCopy=True)
    app.doJavaScript(f'alert("save to jpg: {jpg}")')
  2. How to get the Photoshop program ID

    main

    To interact with Photoshop via COM, you may need its program ID. You can retrieve the Photoshop.Application ID using the following PowerShell command which searches the Windows Registry:

    Get-ChildItem "HKLM:\SOFTWARE\Classes" |
      ?{ ($_.PSChildName -match "^[a-z]+\.[a-z]+(\.\d+)?$") -and ($_.GetSubKeyNames() -contains "CLSID") } |
      ?{ $_.PSChildName -match "Photoshop.Application" } | ft PSChildName
  3. Manage Photoshop sessions using the Session context manager

    main

    The Session class provides a context manager to handle Photoshop automation tasks more cleanly. You can specify an action (such as new_document) when initializing the session. Inside the with block, the session object provides access to the active document and the application instance.

    from photoshop import Session
    
    with Session(action="new_document") as ps:
        doc = ps.active_document
        text_color = ps.SolidColor()
        text_color.rgb.green = 255
        new_text_layer = doc.artLayers.add()
        new_text_layer.kind = ps.LayerKind.TextLayer
        new_text_layer.textItem.contents = 'Hello, World!'
        new_text_layer.textItem.position = [160, 167]
        new_text_layer.textItem.size = 40
        new_text_layer.textItem.color = text_color
        options = ps.JPEGSaveOptions(quality=5)
        jpg = 'd:/hello_world.jpg'
        doc.saveAs(jpg, options, asCopy=True)
        ps.app.doJavaScript(f'alert("save to jpg: {jpg}")')