Automating Simple Photoshop with Python
You can try to use the package photoshop_python_api
.
https://github.com/loonghao/photoshop_python_api
Hello World
for this package.
import photoshop as ps
def hello_world():
app = ps.Application()
doc = app.documents.add()
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)
app.doJavaScript(f'alert("save to jpg: {jpg}")')
if __name__ == '__main__':
hello_world()
More examples:
https://photoshop-python-api.readthedocs.io/en/master/examples.html
jon_groth34
Updated on August 03, 2022Comments
-
jon_groth34 over 1 year
The business I work for creates laser engraved custom products. The engraver machine requires files to be in the psd format. Our website enables users to select a template and enter in what text they want. After they order, one of our workers opens the template in GIMP/Photoshop, switches out the text, then saves and uploads it. Seems simple enough, but when you're doing hundreds of these a day, seems like it's worth automating.
The main language I speak is python, but I've dabbled enough in other languages enough that I can do small projects. I see there are a few different PSD/photoshop modules out there (pywin32, pyps, psd-tools), but there either isn't enough documentation on them or they don't seem suitable for my purposes. In more detail, the only thing that needs to happen is to have a text layer added, where we can choose the font, location, and preferably things like whether or not it's bolded, italicized, etc., but that's not required. I've got a solution that can make a text layer (via win32com), but I can't find how to edit the different elements of the text.
import win32com.client # Pieced together from # http://techarttiki.blogspot.com/2008/08/photoshop-scripting-with-python.html # and # http://rubypane.blogspot.com/2013/01/sample-python-script-to-control.html psApp = win32com.client.Dispatch("Photoshop.Application") psApp.Open(r"C:\temp\blah.psd") # Opens a PSD file doc = psApp.Application.ActiveDocument # Get active document object layer = doc.ArtLayers[2] # Get the bottom-most layer layers = doc.artLayers artLayerRef = layers.add artLayerRef.kind = 2 #Text layer # Set the contents of the text layer. textItemRef = artLayerRef.TextItem textItemRef.Contents = "Hello, web!" doc.Save()
That snippet of code produces a small text box without any formatting saying "Hello World!". Again, I'd like to be able to control where it's located, the font, and if possible, other elements of it as well. I'm also open to other solutions, whether it's creating the picture in another format then converting it to psd, working with another module/language, or any other solution your brilliant minds can think of. Worst case scenario, we keep on doing it manually. Thank you for any help!
-
Sergey Kritskiy almost 5 yearsI don't know python but those properties look a lot like the ones from Photoshop Javascript (adobe.com/content/dam/acom/en/devnet/photoshop/pdfs/…), I wonder if you could try to set an
textItemRef.position
for location,textItemRef.font
for font, etc. Search fortextItem
in the scripting reference pdf. -
Sergey Kritskiy almost 5 years(but apparently properties in python start with a capital letter)
-