LazPaint scripts

From Free Pascal wiki
Revision as of 08:30, 16 April 2020 by Circular (talk | contribs) (act on each layer)
Jump to navigationJump to search

English (en) français (fr)

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

  • Channels: separate the color channels to RGB or HSL.
  • Layer effects: known layer effects like drop shadow, stroke or color overlay.
  • Mask: function to create a mask layer
  • Render: some example of using the tools from a script
  • Version: show the version of Python

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:

# My script

Or if you want it to be in a submenu:

# 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:

from lazpaint import dialog

To use a function from the module:

dialog.show_message("hello world")

So here is our hello world program:

# 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:

  • On Windows: C:\Program Files\LazPaint\scripts
  • On Linux: /usr/share/lazpaint/scripts

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

  • colors: definitions of colors and color filters
  • command: send raw commands and get version of LazPaint
  • dialog: show messages and dialog boxes
  • filters: apply filters (other than color filters)
  • image: new, load, rotate...
  • imagelist: use the imagelist window (get next image, add files)
  • layer: configure layers, access pixels
  • selection: act on the selection (in particular the blue selection)
  • tools: use the tools by providing mouse positions and key strokes
  • view: zoom and grid

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:

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:

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:

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.

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