An image editor with an API (preferably python-oriented)

applicationsdevelopmentimage editing

Is there an image editor for OS X (e.g. Preview) that can be operated programmatically, preferably with python, or that has a built-in programming language, like MS Word has VBA built-in (at least on Windows systems)?

I'd like to be able to do the following with code:

  1. Open an image in the image editor,
  2. Use the editor to save the image to some arbitrary location in the file system.

It is essential for me to save the file via the editor, since when the editor saves a file it does more than simply save it: it also writes meta info, which I am interested in.


macOS Sierra Version 10.12.4

Best Answer

Preview supports AppleScript which allows you to open and save files from CLI. There are commands for open and save.

For example, to open a file with Preview:

tell application "Preview"
    open "/path/to/file.png"
end tell

You can run AppleScript as a script file, or from the command line using osascript -e "…".

osascript -e "tell application \"Preview\"" -e "open \"/path/to/file.png\"" -e "end tell"

You can find the full Preview AppleScript dictionary by opening Script Editor.app (from the Utilities folder), choosing File → Open Dictionary and selecting Preview. This will show you many of the commands that Preview supports with the syntax for how to use them.