Skip to content

Commit

Permalink
Merge pull request #5 from loonghao/improve_code
Browse files Browse the repository at this point in the history
Improve code
  • Loading branch information
loonghao authored Feb 23, 2020
2 parents ac104b4 + f564f90 commit 01cebbe
Show file tree
Hide file tree
Showing 48 changed files with 759 additions and 525 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exclude: "conf.py"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
Expand Down
82 changes: 12 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can install via pip.
pip install photoshop_python_api
```
or through clone from Github.
```cmd
```git exclude
git clone https://github.com/loonghao/photoshop_python_api.git
```
Install package.
Expand All @@ -37,88 +37,30 @@ Hello World

```python

from photoshop import Application
from photoshop import JPEGSaveOptions
from photoshop import SolidColor
from photoshop import LayerKind

app = Application()
import photoshop as ps
app = ps.Application()
doc = app.documents.add()
new_doc = doc.artLayers.add()
text_color = SolidColor()
text_color.rgb.red = 225
text_color = ps.SolidColor()
text_color.rgb.green = 255
text_color.rgb.blue = 0
new_text_layer = new_doc
new_text_layer.kind = LayerKind.BRIGHTNESSCONTRAST
new_text_layer.textItem.contents = "Hello, World!"
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 = JPEGSaveOptions()
options.quality = 10
options = ps.JPEGSaveOptions(quality=5)
# # save to jpg
jpg = 'd:/hello_world.jpg'
doc.saveAs(jpg, options, as_copy=True)
app.eval_javascript('alert("save to jpg: {}")'.format(jpg))
```

Create thumbnail
----------------


```python

from photoshop import Application
from photoshop import JPEGSaveOptions

app = Application()
active_document = app.activeDocument
orig_name = active_document.name
width_str = active_document.width
height_str = active_document.height
index = width_str / 1280
thumb_width = int(width_str / index)
thumb_height = int(height_str / index)
thumb_doc = active_document.duplicate('{}_tumb'.format(orig_name))
thumb_doc.resizeImage(thumb_width, thumb_height)
o = JPEGSaveOptions()
o.quality = 10
thumb_doc.saveAs('c:/thumb.jpg', o, as_copy=True)
thumb_doc.close()
doc.saveAs(jpg, options, asCopy=True)
app.doJavaScript(f'alert("save to jpg: {jpg}")')

```
![demo](https://i.imgur.com/25TrzbV.gif)

Run javascript
--------------

```python

from photoshop import Application

app = Application()
jsx = r"""
var doc = app.activeDocument;
var orig_name = doc.name;
alert(orig_name);
"""
app.eval_javascript(jsx)

```

Open .psd file
--------------

```python

from photoshop import Application

app = Application()
app.load("your/psd/or/psb/file_path.psd")

```

Research and reference
Useful links
----------------------
- https://theiviaxx.github.io/photoshop-docs/Photoshop/
- http://wwwimages.adobe.com/www.adobe.com/content/dam/acom/en/devnet/photoshop/pdfs/photoshop-cc-javascript-ref-2015.pdf
Expand All @@ -129,4 +71,4 @@ Research and reference
- http://www.shining-lucy.com/wiki/page.php?id=appwiki:photoshop:ps_script
- http://www.tonton-pixel.com/wp-content/uploads/DecisionTable.pdf
- http://jongware.mit.edu/pscs5js_html/psjscs5/pc_Application.html
- https://indd.adobe.com/view/a0207571-ff5b-4bbf-a540-07079bd21d75
- https://indd.adobe.com/view/a0207571-ff5b-4bbf-a540-07079bd21d75
Binary file modified docs/_build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/_build/doctrees/installation.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/usage.doctree
Binary file not shown.
Binary file modified docs/_build/html/objects.inv
Binary file not shown.
1 change: 0 additions & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ Or you can install via pip.
.. code-block:: console
$ pip install photoshop_python_api
40 changes: 20 additions & 20 deletions docs/src/photoshop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ photoshop.application module
:undoc-members:
:show-inheritance:

photoshop.art\_layer module
---------------------------
photoshop.artlayer module
-------------------------

.. automodule:: photoshop.art_layer
.. automodule:: photoshop.artlayer
:members:
:undoc-members:
:show-inheritance:

photoshop.art\_layers module
----------------------------
photoshop.artlayers module
--------------------------

.. automodule:: photoshop.art_layers
.. automodule:: photoshop.artlayers
:members:
:undoc-members:
:show-inheritance:
Expand Down Expand Up @@ -100,6 +100,14 @@ photoshop.documents module
:undoc-members:
:show-inheritance:

photoshop.enumerations module
-----------------------------

.. automodule:: photoshop.enumerations
:members:
:undoc-members:
:show-inheritance:

photoshop.errors module
-----------------------

Expand All @@ -116,18 +124,18 @@ photoshop.layer module
:undoc-members:
:show-inheritance:

photoshop.layerSets module
--------------------------
photoshop.layerSet module
-------------------------

.. automodule:: photoshop.layerSets
.. automodule:: photoshop.layerSet
:members:
:undoc-members:
:show-inheritance:

photoshop.layer\_kind module
----------------------------
photoshop.layerSets module
--------------------------

.. automodule:: photoshop.layer_kind
.. automodule:: photoshop.layerSets
:members:
:undoc-members:
:show-inheritance:
Expand Down Expand Up @@ -180,14 +188,6 @@ photoshop.text\_item module
:undoc-members:
:show-inheritance:

photoshop.units module
----------------------

.. automodule:: photoshop.units
:members:
:undoc-members:
:show-inheritance:


Module contents
---------------
Expand Down
68 changes: 4 additions & 64 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,22 @@ Usage
Hello World
-----------

.. code-block:: python
from photoshop import Application
from photoshop import JPEGSaveOptions
from photoshop import SolidColor
from photoshop import LayerKind
app = Application()
doc = app.documents.add()
new_doc = doc.artLayers.add()
text_color = SolidColor()
text_color.rgb.red = 225
text_color.rgb.green = 255
text_color.rgb.blue = 0
new_text_layer = new_doc
new_text_layer.kind = LayerKind.BRIGHTNESSCONTRAST
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 = JPEGSaveOptions()
options.quality = 10
# # save to jpg
jpg = 'd:/hello_world.jpg'
doc.saveAs(jpg, options, as_copy=True)
app.eval_javascript('alert("save to jpg: {}")'.format(jpg))
.. literalinclude:: ../examples/hello_world.py


Create thumbnail
----------------


.. code-block:: python
from photoshop import Application
from photoshop import JPEGSaveOptions
app = Application()
active_document = app.activeDocument
orig_name = active_document.name
width_str = active_document.width
height_str = active_document.height
index = width_str / 1280
thumb_width = int(width_str / index)
thumb_height = int(height_str / index)
thumb_doc = active_document.duplicate('{}_tumb'.format(orig_name))
thumb_doc.resizeImage(thumb_width, thumb_height)
o = JPEGSaveOptions()
o.quality = 10
thumb_doc.saveAs('c:/thumb.jpg', o, as_copy=True)
thumb_doc.close()
.. literalinclude:: ../examples/create_thumbnail.py

Run javascript
--------------

.. code-block:: python
from photoshop import Application
.. literalinclude:: ../examples/eval_javascript.py

app = Application()
jsx = r"""
var doc = app.activeDocument;
var orig_name = doc.name;
alert(orig_name);
"""
app.eval_javascript(jsx)


Open .psd file
--------------

.. code-block:: python
from photoshop import Application
app = Application()
app.load("your/psd/or/psb/file_path.psd")
.. literalinclude:: ../examples/open_psd.py
52 changes: 52 additions & 0 deletions examples/SmartSharpen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""This script demonstrates how you can use the action manager to execute the
Emboss filter.
References:
https://github.com/lohriialo/photoshop-scripting-python/blob/master/SmartSharpen.py
"""

import os
import photoshop as ps


app = ps.Application()

fileName = os.path.join(os.path.dirname(__file__), 'layer_comps.psd')
docRef = app.open(fileName)

nlayerSets = docRef.layerSets
nArtLayers = docRef.layerSets.item(nlayerSets.length)
docRef.activeLayer = nArtLayers.artLayers.item(nArtLayers.artLayers.length)


def SmartSharpen(inAmount, inRadius, inNoise):
idsmart_sharpen_id = app.stringIDToTypeID(ps.smartSharpen)
desc37 = ps.ActionDescriptor()

idpresetKind = app.stringIDToTypeID(ps.presetKind)
idpresetKindType = app.stringIDToTypeID(ps.presetKindType)
idpresetKindCustom = app.stringIDToTypeID(ps.presetKindCustom)
desc37.putEnumerated(idpresetKind, idpresetKindType, idpresetKindCustom)

idAmnt = app.charIDToTypeID(ps.AMNT)
idPrc = app.charIDToTypeID(ps.RDS)
desc37.putUnitDouble(idAmnt, idPrc, inAmount)

idRds = app.charIDToTypeID(ps.RDS)
idPxl = app.charIDToTypeID(ps.PX1)
desc37.putUnitDouble(idRds, idPxl, inRadius)

idnoiseReduction = app.stringIDToTypeID(ps.noiseReduction)
idPrc = app.charIDToTypeID(ps.PRC)
desc37.putUnitDouble(idnoiseReduction, idPrc, inNoise)

idblur = app.charIDToTypeID(ps.blur)
idblurType = app.stringIDToTypeID(ps.blurType)
idGsnB = app.charIDToTypeID(ps.GSNB)
desc37.putEnumerated(idblur, idblurType, idGsnB)

app.ExecuteAction(idsmart_sharpen_id, desc37)


SmartSharpen(300, 2.0, 20)
26 changes: 26 additions & 0 deletions examples/active_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Set the active layer to the last art layer of the active document, or the
# first if the last is already active.


import photoshop as ps

app = ps.Application()
if len(app.documents) < 1:
docRef = app.documents.add()
else:
docRef = app.activeDocument

if len(docRef.layers) < 2:
docRef.artLayers.add()

activeLayerName = docRef.activeLayer.name
SetLayerName = ''

print(docRef.activeLayer.name)
print(docRef.layers.item(len(docRef.layers)))
# Set current active to first layer.
# docRef.activeLayer = docRef.layers.item(1)
new_layer = docRef.artLayers.add()
print(new_layer.name)
new_layer.name = "test"

Loading

0 comments on commit 01cebbe

Please sign in to comment.