LazPaint scripts: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: LazPaint scripts
| Old | New | Line | |
|---|---|---|---|
| 1 | + | [[LazPaint|Go back]] | |
| 2 | + | ||
| 3 | + | Scripts are accessible from the **Script** menu. There are in Python language and stored in the *scripts* folder of the application. | |
| 4 | + | ||
| 5 | + | ## Provided scripts | |
| 6 | + | ||
| 7 | + | - Channels: separate the color channels to RGB or HSL. | |
| 8 | + | - Layer effects: known layer effects like drop shadow, stroke or color overlay. | |
| 9 | + | - Mask: function to create a mask layer | |
| 10 | + | - Render: some example of using the tools from a script | |
| 11 | + | - Version: show the version of Python | |
| 12 | + | ||
| 13 | + | ## How to make a script | |
| 14 | + | ||
| 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 | + | ||
| 17 | + | `# My script` | |
| 18 | + | ||
| 19 | + | Or if you want it to be in a submenu: | |
| 20 | + | ||
| 21 | + | `# My folder > My script` | |
| 22 | + | ||
| 23 | + | You will need modules to communicate with Lazpaint from the script, so for example to show a message you would write: | |
| 24 | + | ||
| 25 | + | `from lazpaint import dialog` | |
| 26 | + | ||
| 27 | + | To use a function from the module: | |
| 28 | + | ||
| 29 | + | `dialog.show_message("hello world")` | |
| 30 | + | ||
| 31 | + | So here is our hello world program: | |
| 32 | + | ||
| 33 | + | `# Tutorial > Hello world program`\ | |
| 34 | + | `from lazpaint import dialog`\ | |
| 35 | + | `dialog.show_message("hello world")` | |
| 36 | + | ||
| 37 | + | ## Run a script | |
| 38 | + | ||
| 39 | + | ### Run from a user folder | |
| 40 | + | ||
| 41 | + | You can save your script in your documents and in LazPaint use Script \> Run script to browse the folders. | |
| 42 | + | ||
| 43 | + | ### Run from Script menu | |
| 44 | + | ||
| 45 | + | 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 | + | ||
| 47 | + | - On Windows: *C:\Program Files\LazPaint\scripts* | |
| 48 | + | - On Linux: */usr/share/lazpaint/scripts* | |
| 49 | + | ||
| 50 | + | 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 | + | ||
| 52 | + | ## Available modules | |
| 53 | + | ||
| 54 | + | - colors: definitions of colors and color filters | |
| 55 | + | - command: send raw commands and get version of LazPaint | |
| 56 | + | - dialog: show messages and dialog boxes | |
| 57 | + | - filters: apply filters (other than color filters) | |
| 58 | + | - image: new, load, rotate... | |
| 59 | + | - imagelist: use the imagelist window (get next image, add files) | |
| 60 | + | - layer: configure layers, access pixels | |
| 61 | + | - selection: act on the selection (in particular the blue selection) | |
| 62 | + | - tools: use the tools by providing mouse positions and key strokes | |
| 63 | + | - view: zoom and grid | |
| 64 | + | ||
| 65 | + | ## Grouping changes | |
| 66 | + | ||
| 67 | + | 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 | + | ||
| 69 | + | `from lazpaint import image, layer, colors`\ | |
| 70 | + | `image.do_begin() # start undo block`\ | |
| 71 | + | `layer.new() # first action: new layer`\ | |
| 72 | + | `layer.fill(colors.YELLOW) # second action: fill with yellow`\ | |
| 73 | + | `image.do_end() # end undo block` | |
| 74 | + | ||
| 75 | + | ## Interact with Pillow | |
| 76 | + | ||
| 77 | + | 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 | + | ||
| 79 | + | `from lazpaint import image, layer`\ | |
| 80 | + | `from PIL import Image`\ | |
| 81 | + | `temp_name = image.get_temporary_name()`\ | |
| 82 | + | `temp_name = layer.save_as(temp_name) # export to disk`\ | |
| 83 | + | `im = Image.open(temp_name) # load into Pillow` | |
| 84 | + | ||
| 85 | + | Then you can transfer back the image to LazPaint: | |
| 86 | + | ||
| 87 | + | `im.save(temp_name, "PNG") # export to disk`\ | |
| 88 | + | `layer.add_from_file(temp_name) # import from disk` | |
| 89 | + | ||
| 90 | + | ## Do an action on each layer | |
| 91 | + | ||
| 92 | + | The module image contains the function *iterate_layers* which selects each layer one after another. | |
| 93 | + | ||
| 94 | + | `from lazpaint import image, colors`\ | |
| 95 | + | `image.do_begin()`\ | |
| 96 | + | `for cur_layer_id in image.iterate_layers():`\ | |
| 97 | + | ` colors.complementary()`\ | |
| 98 | + | `image.do_end()` | |
| 99 | + | ||
| 100 | + | ## Color values | |
| 101 | + | ||
| 102 | + | 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 | + | ||
| 104 | + | 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 | + | ||
| 106 | + | `from lazpaint import colors, dialog, tools`\ | |
| 107 | + | `dodger_blue = colors.RGB(30, 144, 255)`\ | |
| 108 | + | `half_transparent_black = colors.RGBA(0, 0, 0, 128)`\ | |
| 109 | + | `dialog.show_message(dodger_blue.blue) # displays 255`\ | |
| 110 | + | `tools.set_fore_color(dodger_blue) # select dodger blue as pen color` | |
| 111 | + | ||
| 112 | + | --- | |
| 113 | + | *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).* |
