Răsfoiți Sursa

reorganised scripts

circular17 5 ani în urmă
părinte
comite
1765736eb3

+ 0 - 63
lazpaintscripts/lazpaint.py

@@ -1,63 +0,0 @@
-from lazpaint_colors import *
-from lazpaint_command import command
-
-DM_DRAW = "dmDrawWithTransparency"
-DM_LINEAR = "dmLinearBlend"
-DM_SET = "dmSet"
-DM_SET_EXCEPT_TRANSPARENT = "dmSetExceptTransparent"
-DM_XOR = "dmXor"
-
-def file_new(width, height, color=TRANSPARENT, ignore_modified=False):
-  command("FileNew", Width=width, Height=height, BackColor=color, IgnoreModified=ignore_modified)
-
-def file_open(file_name=None, ignore_modified=False):
-  command("FileOpen", FileName=file_name, IgnoreModified=ignore_modified)
-
-def file_save(skip_options=False):
-  return command("FileSave?", SkipOptions=skip_options)
-
-def file_save_as(file_name=None, validate=False, overwrite=False, skip_options=False):
-  return command("FileSaveAs?", FileName=file_name, Validate=validate, Overwrite=overwrite, SkipOptions=skip_options) 
-
-def file_reload(ignore_modified=False):
-  command("FileReload", IgnoreModified=ignore_modified)
-
-def file_load_selection(file_name=None):
-  command("FileLoadSelection", FileName=file_name)
-
-def file_save_selection_as(file_name=None):
-  return command("FileSaveSelectionAs?", FileName=file_name)
-
-def get_file_name():
-  return command("GetFileName?") 
-
-def put_image(x, y, image, mode=DM_DRAW, opacity=255):
-  height = len(image)
-  if height == 0: return
-  width = max([len(scanline) for scanline in image])
-  flattened = ""
-  for scanline in image:
-    flattened += "".join([str(color) for color in scanline]) + "00000000" * (width - len(scanline))
-  command("PutImage", X=x, Y=y, Width=width, Height=height, Data=flattened, Mode=mode, Opacity=opacity)
-
-def get_pixel(x, y):
-  return str_to_RGBA(command("GetPixel?", X=x, Y=y))
-
-def layer_fill(color, mode=DM_DRAW):
-  command("LayerFill", Color=color, Mode=mode)
-
-def get_image_width():
-  return command("GetImageWidth?")
-
-def get_image_height():
-  return command("GetImageHeight?")
-
-def show_message(message):
-  command("ShowMessage?", Message=message)
-
-def get_layer_count():
-  return command("GetLayerCount?")
-
-if __name__ == "__main__":
-  show_message("This is the script library.")
-

+ 4 - 0
lazpaintscripts/lazpaint_colors.py → lazpaintscripts/lazpaint/colors.py

@@ -1,4 +1,8 @@
 import collections
+from lazpaint import command
+
+if __name__ == "__main__":
+  command.show_message("Library defining colors.")
 
 CustomRGBA = collections.namedtuple("RGBA", "red, green, blue, alpha")
 class RGBA(CustomRGBA):

+ 7 - 1
lazpaintscripts/lazpaint_command.py → lazpaintscripts/lazpaint/command.py

@@ -7,7 +7,7 @@ if input('') != chr(27) + 'LazPaint':
   exit()
 
 # sends a command to LazPaint
-def command(command, **keywords):
+def send(command, **keywords):
   if keywords is None:
     print(chr(27) + command)
   else:
@@ -16,3 +16,9 @@ def command(command, **keywords):
     return ast.literal_eval(input(''))
   else:
     return
+
+def show_message(message):
+  send("ShowMessage?", Message=message)
+
+if __name__ == "__main__":
+  show_message("Library to communicate with LazPaint.")

+ 16 - 0
lazpaintscripts/lazpaint/image.py

@@ -0,0 +1,16 @@
+from lazpaint import command, colors
+
+if __name__ == "__main__":
+  command.show_message("Library to act on the whole image.")
+
+def new(width, height, color=colors.TRANSPARENT, ignore_modified=False):
+  command.send("FileNew", Width=width, Height=height, BackColor=color, IgnoreModified=ignore_modified)
+
+def get_width():
+  return command.send("GetImageWidth?")
+
+def get_height():
+  return command.send("GetImageHeight?")
+
+def get_layer_count():
+  return command.send("GetLayerCount?")

+ 26 - 0
lazpaintscripts/lazpaint/io.py

@@ -0,0 +1,26 @@
+from lazpaint import command
+
+if __name__ == "__main__":
+  command.show_message("Library to load and save files.")
+
+def open(file_name=None, ignore_modified=False):
+  command.send("FileOpen", FileName=file_name, IgnoreModified=ignore_modified)
+
+def save(skip_options=False):
+  return command.send("FileSave?", SkipOptions=skip_options)
+
+def save_as(file_name=None, validate=False, overwrite=False, skip_options=False):
+  return command.send("FileSaveAs?", FileName=file_name, Validate=validate, Overwrite=overwrite, SkipOptions=skip_options) 
+
+def reload(ignore_modified=False):
+  command.send("FileReload", IgnoreModified=ignore_modified)
+
+def load_selection(file_name=None):
+  command.send("FileLoadSelection", FileName=file_name)
+
+def save_selection_as(file_name=None):
+  return command.send("FileSaveSelectionAs?", FileName=file_name)
+
+def get_name():
+  return command.send("GetFileName?")
+

+ 25 - 0
lazpaintscripts/lazpaint/layer.py

@@ -0,0 +1,25 @@
+from lazpaint import command, colors
+
+if __name__ == "__main__":
+  command.show_message("Library to access layer content.")
+
+DM_DRAW = "dmDrawWithTransparency"
+DM_LINEAR = "dmLinearBlend"
+DM_SET = "dmSet"
+DM_SET_EXCEPT_TRANSPARENT = "dmSetExceptTransparent"
+DM_XOR = "dmXor"
+
+def put_image(x, y, image, mode=DM_DRAW, opacity=255):
+  height = len(image)
+  if height == 0: return
+  width = max([len(scanline) for scanline in image])
+  flattened = ""
+  for scanline in image:
+    flattened += "".join([str(color) for color in scanline]) + "00000000" * (width - len(scanline))
+  command.send("PutImage", X=x, Y=y, Width=width, Height=height, Data=flattened, Mode=mode, Opacity=opacity)
+
+def get_pixel(x, y):
+  return colors.str_to_RGBA(command.send("GetPixel?", X=x, Y=y))
+
+def fill(color, mode=DM_DRAW):
+  command.send("LayerFill", Color=color, Mode=mode)

+ 14 - 11
lazpaintscripts/test_file.py

@@ -1,13 +1,16 @@
-from lazpaint import *
+from lazpaint import image, io, colors, layer
+
+selection_name = io.save_selection_as("script_test_selection.png")
+image.new(100, 100, colors.RED)
+io.load_selection(selection_name)
+io.show_message("Selection restored")
 
-selection_file_name = file_save_selection_as("script_test_selection.png")
-file_new(100, 100, RED)
-file_load_selection(selection_file_name)
-show_message("Selection restored")
 file_name = file_save_as("script_test_file.png", skip_options=True)
-file_new(100, 100, LIME)
-file_save_as(file_name, validate=True, overwrite=True, skip_options=True)
-layer_fill(BLUE)
-file_reload(ignore_modified=True)
-layer_fill(PURPLE)
-file_save(skip_options=True)
+image.new(100, 100, colors.LIME)
+io.save_as(file_name, validate=True, overwrite=True, skip_options=True)
+
+layer.fill(colors.BLUE)
+io.reload(ignore_modified=True)
+
+layer.fill(colors.PURPLE)
+io.save(skip_options=True)

+ 17 - 0
lazpaintscripts/test_pixel.py

@@ -0,0 +1,17 @@
+from lazpaint import image, layer, command, colors
+
+width = 256
+height = 256
+image.new(width, height)
+image = []
+for y in range(height):
+  scanline = [colors.RGB(0,x,y) for x in range(width)]
+  image.append(scanline)
+
+layer.put_image(0, 0, image, layer.DM_SET)
+if layer.get_pixel(192,64).green != 192:
+  command.show_message("The value of the pixel is not correct.")
+else:
+  command.show_message("Test successful.")
+  
+

+ 0 - 13
lazpaintscripts/test_put_image.py

@@ -1,13 +0,0 @@
-from lazpaint import *
-
-width = 256
-height = 256
-file_new(width, height)
-layer_fill(WHITE)
-image = []
-for y in range(height):
-  scanline = [RGB(0,x,y) for x in range(width)]
-  image.append(scanline)
-
-put_image(0, 0, image, DM_SET)
-