Sign in Join
Wiki copytemporary

LazPaint scripts: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11[[LazPaint|Go back]]
22
33Scripts are accessible from the **Script** menu. There are in Python language and stored in the *scripts* folder of the application.
44
55## Provided scripts
66
77- Channels: separate the color channels to RGB or HSL.
88- Layer effects: known layer effects like drop shadow, stroke or color overlay.
99- Mask: function to create a mask layer
1010- Render: some example of using the tools from a script
1111- Version: show the version of Python
1212
1313## How to make a script
1414
1515A 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:
1616
17-`# My script`
17+```text
18+# My script
19+```
1820
1921Or if you want it to be in a submenu:
2022
21-`# My folder > My script`
23+```text
24+# My folder > My script
25+```
2226
2327You will need modules to communicate with Lazpaint from the script, so for example to show a message you would write:
2428
25-`from lazpaint import dialog`
29+```text
30+from lazpaint import dialog
31+```
2632
2733To use a function from the module:
2834
29-`dialog.show_message("hello world")`
35+```text
36+dialog.show_message("hello world")
37+```
3038
3139So here is our hello world program:
3240
33-`# Tutorial > Hello world program`\
34-`from lazpaint import dialog`\
35-`dialog.show_message("hello world")`
41+``` pascal
42+# Tutorial > Hello world program
43+from lazpaint import dialog
44+dialog.show_message("hello world")
45+```
3646
3747## Run a script
3848
3949### Run from a user folder
4050
4151You can save your script in your documents and in LazPaint use Script \> Run script to browse the folders.
4252
4353### Run from Script menu
4454
4555Alternatively 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:
4656
4757- On Windows: *C:\Program Files\LazPaint\scripts*
4858- On Linux: */usr/share/lazpaint/scripts*
4959
5060Note that the first line must start with a \# and have no spacing before. You need to restart LazPaint to update the Script menu.
5161
5262## Available modules
5363
5464- colors: definitions of colors and color filters
5565- command: send raw commands and get version of LazPaint
5666- dialog: show messages and dialog boxes
5767- filters: apply filters (other than color filters)
5868- image: new, load, rotate...
5969- imagelist: use the imagelist window (get next image, add files)
6070- layer: configure layers, access pixels
6171- selection: act on the selection (in particular the blue selection)
6272- tools: use the tools by providing mouse positions and key strokes
6373- view: zoom and grid
6474
6575## Grouping changes
6676
6777Generally 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:
6878
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`
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+```
7486
7587## Interact with Pillow
7688
7789If 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:
7890
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`
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+```
8498
8599Then you can transfer back the image to LazPaint:
86100
87-`im.save(temp_name, "PNG") # export to disk`\
88-`layer.add_from_file(temp_name) # import from disk`
101+```text
102+im.save(temp_name, "PNG") # export to disk
103+layer.add_from_file(temp_name) # import from disk
104+```
89105
90106## Do an action on each layer
91107
92108The module image contains the function *iterate_layers* which selects each layer one after another.
93109
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()`
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+```
99117
100118## Color values
101119
102120Colors 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.
103121
104122The 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.
105123
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`
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+```
111131
112132---
113133*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).*

Back to history