LazPaint scripts: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | [[LazPaint|Go back]] | |
| 2 | 2 | ||
| 3 | 3 | Scripts are accessible from the **Script** menu. There are in Python language and stored in the *scripts* folder of the application. | |
| 4 | 4 | ||
| 5 | 5 | ## Provided scripts | |
| 6 | 6 | ||
| 7 | 7 | - Channels: separate the color channels to RGB or HSL. | |
| 8 | 8 | - Layer effects: known layer effects like drop shadow, stroke or color overlay. | |
| 9 | 9 | - Mask: function to create a mask layer | |
| 10 | 10 | - Render: some example of using the tools from a script | |
| 11 | 11 | - Version: show the version of Python | |
| 12 | 12 | ||
| 13 | 13 | ## How to make a script | |
| 14 | 14 | ||
| 15 | 15 | A script is a text file with the extension .py that contains the code to run. The first line is a comment that contains the title of the script: | |
| 16 | 16 | ||
| 17 | - | ||
| 17 | + | ```text | |
| 18 | + | # My script | |
| 19 | + | ``` | |
| 18 | 20 | ||
| 19 | 21 | Or if you want it to be in a submenu: | |
| 20 | 22 | ||
| 21 | - | ||
| 23 | + | ```text | |
| 24 | + | # My folder > My script | |
| 25 | + | ``` | |
| 22 | 26 | ||
| 23 | 27 | You will need modules to communicate with Lazpaint from the script, so for example to show a message you would write: | |
| 24 | 28 | ||
| 25 | - | ||
| 29 | + | ```text | |
| 30 | + | from lazpaint import dialog | |
| 31 | + | ``` | |
| 26 | 32 | ||
| 27 | 33 | To use a function from the module: | |
| 28 | 34 | ||
| 29 | - | ||
| 35 | + | ```text | |
| 36 | + | dialog.show_message("hello world") | |
| 37 | + | ``` | |
| 30 | 38 | ||
| 31 | 39 | So here is our hello world program: | |
| 32 | 40 | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 41 | + | ``` pascal | |
| 42 | + | # Tutorial > Hello world program | |
| 43 | + | from lazpaint import dialog | |
| 44 | + | dialog.show_message("hello world") | |
| 45 | + | ``` | |
| 36 | 46 | ||
| 37 | 47 | ## Run a script | |
| 38 | 48 | ||
| 39 | 49 | ### Run from a user folder | |
| 40 | 50 | ||
| 41 | 51 | You can save your script in your documents and in LazPaint use Script \> Run script to browse the folders. | |
| 42 | 52 | ||
| 43 | 53 | ### Run from Script menu | |
| 44 | 54 | ||
| 45 | 55 | Alternatively you can add you script to the menu of LazPaint. To do that, using administrator rights, copy the script file into the *scripts* folder of LazPaint: | |
| 46 | 56 | ||
| 47 | 57 | - On Windows: *C:\Program Files\LazPaint\scripts* | |
| 48 | 58 | - On Linux: */usr/share/lazpaint/scripts* | |
| 49 | 59 | ||
| 50 | 60 | Note that the first line must start with a \# and have no spacing before. You need to restart LazPaint to update the Script menu. | |
| 51 | 61 | ||
| 52 | 62 | ## Available modules | |
| 53 | 63 | ||
| 54 | 64 | - colors: definitions of colors and color filters | |
| 55 | 65 | - command: send raw commands and get version of LazPaint | |
| 56 | 66 | - dialog: show messages and dialog boxes | |
| 57 | 67 | - filters: apply filters (other than color filters) | |
| 58 | 68 | - image: new, load, rotate... | |
| 59 | 69 | - imagelist: use the imagelist window (get next image, add files) | |
| 60 | 70 | - layer: configure layers, access pixels | |
| 61 | 71 | - selection: act on the selection (in particular the blue selection) | |
| 62 | 72 | - tools: use the tools by providing mouse positions and key strokes | |
| 63 | 73 | - view: zoom and grid | |
| 64 | 74 | ||
| 65 | 75 | ## Grouping changes | |
| 66 | 76 | ||
| 67 | 77 | Generally you would like the script to be undoable by clicking only once on Undo. To do that you need to group the changes. For example: | |
| 68 | 78 | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 79 | + | ``` pascal | |
| 80 | + | from lazpaint import image, layer, colors | |
| 81 | + | image.do_begin() # start undo block | |
| 82 | + | layer.new() # first action: new layer | |
| 83 | + | layer.fill(colors.YELLOW) # second action: fill with yellow | |
| 84 | + | image.do_end() # end undo block | |
| 85 | + | ``` | |
| 74 | 86 | ||
| 75 | 87 | ## Interact with Pillow | |
| 76 | 88 | ||
| 77 | 89 | If you want to modify the image using a module like [Pillow](https://python-pillow.org/), you can transfer the image to the module using a temporary file: | |
| 78 | 90 | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 91 | + | ```text | |
| 92 | + | from lazpaint import image, layer | |
| 93 | + | from PIL import Image | |
| 94 | + | temp_name = image.get_temporary_name() | |
| 95 | + | temp_name = layer.save_as(temp_name) # export to disk | |
| 96 | + | im = Image.open(temp_name) # load into Pillow | |
| 97 | + | ``` | |
| 84 | 98 | ||
| 85 | 99 | Then you can transfer back the image to LazPaint: | |
| 86 | 100 | ||
| 87 | - | ||
| 88 | - | ||
| 101 | + | ```text | |
| 102 | + | im.save(temp_name, "PNG") # export to disk | |
| 103 | + | layer.add_from_file(temp_name) # import from disk | |
| 104 | + | ``` | |
| 89 | 105 | ||
| 90 | 106 | ## Do an action on each layer | |
| 91 | 107 | ||
| 92 | 108 | The module image contains the function *iterate_layers* which selects each layer one after another. | |
| 93 | 109 | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 110 | + | ```text | |
| 111 | + | from lazpaint import image, colors | |
| 112 | + | image.do_begin() | |
| 113 | + | for cur_layer_id in image.iterate_layers(): | |
| 114 | + | colors.complementary() | |
| 115 | + | image.do_end() | |
| 116 | + | ``` | |
| 99 | 117 | ||
| 100 | 118 | ## Color values | |
| 101 | 119 | ||
| 102 | 120 | Colors are stored in an instance of the class colors.RGBA. It has 4 fields: red, green, blue and alpha which are between 0 and 255. Those are in [sRGB](https://en.wikipedia.org/wiki/SRGB) colorspace. | |
| 103 | 121 | ||
| 104 | 122 | The functions colors.to_linear and colors.to_std transforms a channel value from sRGB and from linear RGB. Linear values are floating point values between 0 and 1. | |
| 105 | 123 | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 124 | + | ```text | |
| 125 | + | from lazpaint import colors, dialog, tools | |
| 126 | + | dodger_blue = colors.RGB(30, 144, 255) | |
| 127 | + | half_transparent_black = colors.RGBA(0, 0, 0, 128) | |
| 128 | + | dialog.show_message(dodger_blue.blue) # displays 255 | |
| 129 | + | tools.set_fore_color(dodger_blue) # select dodger blue as pen color | |
| 130 | + | ``` | |
| 111 | 131 | ||
| 112 | 132 | --- | |
| 113 | 133 | *From the Free Pascal wiki, page "LazPaint scripts", as it stood on 2020-04-16 after a change by Circular, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
