Work with a picture
Makes, edits, saves, captures, reads text from and finds pictures, on the clipboard, in a named clipboard or in a file.
Work on a picture: op
Keyboard Maestro: NewImage, DrawImage, CompositeImage, ResizeImage, TransformImage, TrimImage, GetImageSize, DisplayImage, ScreenCapture, OCRImage, FindImage
Details #
One step with many operations on a picture. op says which. Most ops follow the same pattern: read the picture from a place, change it, and put it back in the same place.
Where the picture lives. target chooses the place:
clipboard (the default) is the system clipboard. A copied picture is read from it; the result is written back as a PNG.
named is a named clipboard from Switchboard's clipboard history, with its name in
name. Needs clipboard history running.file is a picture file at
path. The change is written back to that file, in the format its extension picks.
Running variables. Every op that handles a picture sets the variable image to an object with width, height, dpiX and dpiY. composite also sets sourceImage for the second picture. find sets foundImage.
Ops
new makes a picture of
widthbyheight, filled withred,green,blue(0 to 255, default 255) andalpha(percent opaque, default 100).draw draws a
shape(line,oval,rectangle,roundedRectangleortext) atx,ywithwidthandheight.strokeandfillare colours,thicknessthe line width (default 1), and fortextthetext,fontandsize(default 18). The default stroke is opaque black and the default fill is clear.composite lays a second picture over the first at
x,y. The second comes fromfrom(clipboard,namedorfile, withfromNameorfromPath).alphais its opacity in percent (default 100) andblendnames a blend mode such asmultiply,screen,overlay,darken,lighten,softLight,hardLight,differenceorexclusion. An unknown blend is treated as normal.scale stretches to exactly
widthbyheight, or bypercentof the size it has (50 is half size, and nowidthorheightis needed). scaleToFit fits inside that box and keeps the proportions. scaleTo is another spelling ofscale.canvas changes the canvas size to
widthbyheight, placing the picture atx,y(left,topalso work).margins adds
left,top,rightandbottomspace.crop keeps the rectangle at
x,yofwidthbyheight.setDPI sets the dots per inch to
dpiXanddpiY(dpisets both). It changes the picture's resolution tag, not its pixels.rotate turns the picture by
degrees, clockwise (rotationis another spelling).flip mirrors it.
directionishorizontal(the default) orvertical.trim cuts away the border that matches
color(the top-left pixel if you give none).fuzzis a percentage of tolerance, 0 to 100.size stores the width in the variable named by
widthVariableand the height inheightVariable. Both are optional; the output is the size object.display opens the picture in your default image viewer (a temporary PNG).
save writes the picture to
path(or tooutputwhen you give one; thenpathis the picture it reads, withtarget: file).format(jpeg,jpg,png,tiff,gif,heicorbmp) overrides the file extension. Missing folders are created. The output is the path.load reads the file at
pathinto the target. A picture file becomes the picture; any other file becomes its text (and goes on the clipboard or into a named clipboard).capture takes a screenshot into the target.
areapicks what:all,main,window, or a rectangle{"type": "rect", "x": 0, "y": 0, "width": 400, "height": 300}in screen points.cursorincludes the mouse pointer.ocr reads the text in a picture.
fromisscreen(the default),clipboard,namedorfile.arealimits a screen read andlanguagesis a list of language codes. The text is the output and goes ininto.find looks for a saved picture on screen. The picture and settings are given in
locator(imagefile name,threshold,scope,pick).areanarrows the search. The output,foundImageandintoholdleft,top,width,heightandscore.
Colours accept an object like {"red": 255, "green": 0, "blue": 0, "alpha": 100}, or text: #RRGGBB, or three or four numbers separated by commas or spaces.
Parameters #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
op | one of | Yes | new draw composite scale scaleTo scaleToFit canvas margins crop setDPI rotate flip trim size display capture ocr find save load | Required. One of new, draw, composite, scale, scaleTo, scaleToFit, canvas, margins, crop, setDPI, rotate, flip, trim, size, display, capture, ocr, find, save, load. action is another spelling. |
action | text | |||
alpha | any value | |||
area | any value | A screen area for capture, ocr and find: all, main, window or a rect object. | ||
blend | text | For composite, the blend mode. | ||
blue | any value | |||
bottom | any value | |||
color | any value | , fuzz: For trim. | ||
cursor | any value | For capture, include the mouse pointer. | ||
degrees | any value | , rotation: For rotate, clockwise. | ||
direction | one of | horizontal vertical | For flip: horizontal or vertical. | |
dpi | any value | |||
dpiX | any value | , dpiY, dpi: For setDPI. | ||
dpiY | any value | |||
fill | any value | |||
font | text | |||
format | one of | jpeg jpg png tiff gif heic bmp | For save: jpeg, jpg, png, tiff, gif, heic or bmp. | |
from | text | , fromName, fromPath: For composite, where the second picture comes from. | ||
fromName | text | |||
fromPath | text | |||
fuzz | any value | |||
green | any value | |||
height | any value | |||
heightVariable | text | |||
image | text | , threshold, scope, pick, role: The same settings as in a locator, accepted at the top level of the params. image is the file name, threshold the match level (0.3 to 0.99, default 0.85), scope is screen or window, and pick is best, top, bottom, left or right. | ||
into | text | A variable for the result of ocr (the text) or find (the match). | ||
languages | list of text | For ocr, a list of language codes. | ||
left | any value | , top, right, bottom: Margin sizes for margins. left and top also work as x and y for draw, composite and canvas. | ||
locator | record | For find, the picture to look for. | ||
name | text | The named clipboard's name, for target: named. | ||
output | text | With target: file: write the result here and leave the source picture as it was. For save, where the picture goes. | ||
path | text | The file path, for target: file, and for save and load. ~ is expanded. | ||
percent | number | For scale: the new size as a percent of the old. Instead of width and height. | ||
pick | text | |||
red | any value | , green, blue, alpha: The fill colour for new. Alpha is percent opaque. | ||
required | any value | For find. true (the default) fails when the picture is not on screen. false stores {"found": false} and carries on. | ||
right | any value | |||
role | text | |||
rotation | any value | |||
scope | text | |||
shape | one of | line oval rectangle roundedRectangle text | For draw: line, oval, rectangle, roundedRectangle or text. | |
size | any value | |||
stroke | any value | , fill: Colours for draw, as an object or text. | ||
target | one of | clipboard named file | Where the picture is: clipboard (default), named or file. | |
text | text | , font, size: The text, font name and point size for draw with shape: text. | ||
thickness | any value | Line width for draw. | ||
threshold | any value | |||
top | any value | |||
width | any value | , height: Sizes in pixels for new, draw, scale, scaleToFit, canvas and crop. | ||
widthVariable | text | , heightVariable: Variables that receive the size, for size. | ||
x | any value | , y: A position. For draw, composite, canvas and crop. | ||
y | any value |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
- Picture: new
- Picture: size
- Show “Made a {{w}} by {{h}} picture”
Read the text on the screen (a dry run only simulates the capture):
- Read the text in a picture
- Show “{{screenText}}”
Gotchas #
capture,ocrfrom the screen, andfindneed Screen Recording permission for Switchboard Engine.findfails withthe picture <file> isn't on screen (no match above 85%; the nearest was NN%)when nothing matches andrequiredis on. Lowerthreshold, or check the right window is showing. It fails with a missing or unreadable picture error if the saved picture is gone.target: clipboardfails withthe clipboard doesn't hold a picturewhen you copied text or files.target: namedneeds clipboard history running and a clipboard with that name.Missing numbers fail with
image <op> needs <name> (a number). Missingdegrees,pathor a badshape,direction,areatype ortargetalso fail with a message naming the problem.An unknown
opfails and lists the valid ones.loadputs a non-picture file on a clipboard; it cannot put it into another file.target: fileoverwrites the file with the changed picture. Keep a copy if you need the original.ocrreturns an empty result, and the note saysno text found, when the picture has no readable text.In a dry run every
imagestep is simulated, so no picture is read or written.