Sign in Join
Wiki copytemporary

LazPaint scripts

English · Français

Go back

Scripts are accessible from the Script menu. There are in Python language and stored in the scripts folder of the application.

Provided scripts

How to make a script

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:

text
# My script

Or if you want it to be in a submenu:

text
# My folder > My script

You will need modules to communicate with Lazpaint from the script, so for example to show a message you would write:

text
from lazpaint import dialog

To use a function from the module:

text
dialog.show_message("hello world")

So here is our hello world program:

pascal
# Tutorial > Hello world program
from lazpaint import dialog
dialog.show_message("hello world")

Run a script

Run from a user folder

You can save your script in your documents and in LazPaint use Script > Run script to browse the folders.

Run from Script menu

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:

Note that the first line must start with a # and have no spacing before. You need to restart LazPaint to update the Script menu.

Available modules

Grouping changes

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:

pascal
from lazpaint import image, layer, colors
image.do_begin()           # start undo block
layer.new()                # first action: new layer
layer.fill(colors.YELLOW)  # second action: fill with yellow
image.do_end()             # end undo block

Interact with Pillow

If you want to modify the image using a module like Pillow, you can transfer the image to the module using a temporary file:

text
from lazpaint import image, layer
from PIL import Image
temp_name = image.get_temporary_name()
temp_name = layer.save_as(temp_name) # export to disk
im = Image.open(temp_name) # load into Pillow

Then you can transfer back the image to LazPaint:

text
im.save(temp_name, "PNG") # export to disk
layer.add_from_file(temp_name) # import from disk

Do an action on each layer

The module image contains the function iterate_layers which selects each layer one after another.

text
from lazpaint import image, colors
image.do_begin()
for cur_layer_id in image.iterate_layers():
  colors.complementary()
image.do_end()

Color values

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 colorspace.

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.

text
from lazpaint import colors, dialog, tools
dodger_blue = colors.RGB(30, 144, 255)
half_transparent_black = colors.RGBA(0, 0, 0, 128)
dialog.show_message(dodger_blue.blue) # displays 255
tools.set_fore_color(dodger_blue) # select dodger blue as pen color

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.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.