layer.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from lazpaint import command, dialog, colors
  2. if __name__ == "__main__":
  3. dialog.show_message("Library to access layer content.")
  4. DM_DRAW = "dmDrawWithTransparency"
  5. DM_LINEAR = "dmLinearBlend"
  6. DM_SET = "dmSet"
  7. DM_SET_EXCEPT_TRANSPARENT = "dmSetExceptTransparent"
  8. DM_XOR = "dmXor"
  9. BLEND_DRAW = 'Transparent'
  10. BLEND_LINEAR = 'LinearBlend'
  11. BLEND_LIGHTEN = 'Lighten'
  12. BLEND_SCREEN = 'Screen'
  13. BLEND_ADD = 'Additive'
  14. BLEND_LINEAR_ADD = 'LinearAdd'
  15. BLEND_COLOR_DODGE = 'ColorDodge'
  16. BLEND_DIVIDE = 'Divide'
  17. BLEND_NICE_GLOW = 'NiceGlow'
  18. BLEND_SOFT_LIGHT = 'SoftLight'
  19. BLEND_HARD_LIGHT = 'HardLight'
  20. BLEND_GLOW = 'Glow'
  21. BLEND_REFLECT = 'Reflect'
  22. BLEND_OVERLAY = 'Overlay'
  23. BLEND_DARK_OVERLAY = 'DarkOverlay'
  24. BLEND_DARKEN = 'Darken'
  25. BLEND_MULTIPLY = 'Multiply'
  26. BLEND_COLOR_BURN = 'ColorBurn'
  27. BLEND_DIFFERENCE = 'Difference'
  28. BLEND_LINEAR_DIFFERENCE = 'LinearDifference'
  29. BLEND_EXCLUSION = 'Exclusion'
  30. BLEND_LINEAR_EXCLUSION = 'LinearExclusion'
  31. BLEND_SUBTRACT = 'Subtract'
  32. BLEND_LINEAR_SUBTRACT = 'LinearSubtract'
  33. BLEND_SUBTRACT_INVERSE = 'SubtractInverse'
  34. BLEND_LINEAR_SUBTRACT_INVERSE = 'LinearSubtractInverse'
  35. BLEND_NEGATION = 'Negation'
  36. BLEND_LINEAR_NEGATION = 'LinearNegation'
  37. BLEND_XOR = 'Xor'
  38. BLEND_SVG_SOFT_LIGHT = 'SvgSoftLight'
  39. BLEND_MASK = 'Mask'
  40. BLEND_LINEAR_MULTIPLY_SATURATION = 'LinearMultiplySaturation'
  41. BLEND_LINEAR_HUE = 'LinearHue'
  42. BLEND_LINEAR_COLOR = 'LinearColor'
  43. BLEND_LINEAR_LIGHTNESS = 'LinearLightness'
  44. BLEND_LINEAR_SATURATION = 'LinearSaturation'
  45. BLEND_CORRECTED_HUE = 'CorrectedHue'
  46. BLEND_CORRECTED_COLOR = 'CorrectedColor'
  47. BLEND_CORRECTED_LIGHTNESS = 'CorrectedLightness'
  48. BLEND_CORRECTED_SATURATION = 'CorrectedSaturation'
  49. def get_id():
  50. return command.send("LayerGetId?")
  51. def get_name() -> str:
  52. return command.send("LayerGetName?")
  53. def get_opacity() -> int:
  54. return command.send("LayerGetOpacity?")
  55. def get_blend_op():
  56. return command.send("LayerGetBlendOp?")
  57. def get_visible() -> bool:
  58. return command.send("LayerGetVisible?")
  59. def select_id(id):
  60. command.send("LayerSelectId", Id=id)
  61. def set_name(name: str):
  62. return command.send("LayerSetName", Name=str(name))
  63. def set_opacity(opacity: int):
  64. return command.send("LayerSetOpacity", Opacity=opacity)
  65. def set_blend_op(blend_op):
  66. return command.send("LayerSetBlendOp", BlendOp=blend_op)
  67. def set_visible(visible: bool):
  68. return command.send("LayerSetVisible", Visible=visible)
  69. def new(name: str = None): #-> id
  70. layer_id = command.send("LayerAddNew?")
  71. if name is not None:
  72. set_name(name)
  73. return layer_id
  74. def paste_as_new(): #-> id
  75. return command.send("EditPasteAsNewLayer?")
  76. def add_from_file(file_name): #-> [id]
  77. return command.send("LayerFromFile?", FileName=file_name)
  78. def save_as(file_name:str, format:str = None): #-> str
  79. return command.send("LayerSaveAs?", FileName=file_name, Format=format)
  80. def duplicate(): #-> id
  81. return command.send("LayerDuplicate?")
  82. def merge_over():
  83. command.send("LayerMergeOver")
  84. def is_empty() -> bool:
  85. return command.send("IsLayerEmpty?")
  86. def is_transparent() -> bool:
  87. return command.send("IsLayerTransparent?")
  88. def get_registry(identifier):
  89. str_result = command.send("LayerGetRegistry?", Identifier=identifier)
  90. if str_result == "":
  91. return None
  92. else:
  93. return command.parse_str(str_result)
  94. def set_registry(identifier, value):
  95. if value == None:
  96. value = ""
  97. elif isinstance(value, str):
  98. value = repr(value)
  99. command.send("LayerSetRegistry", Identifier=identifier, Value=value)
  100. def remove():
  101. command.send("LayerRemoveCurrent")
  102. def get_count() -> int:
  103. return command.send("GetLayerCount?")
  104. def rasterize():
  105. command.send("LayerRasterize")
  106. def get_image_width(image) -> int:
  107. return max([len(scanline) for scanline in image])
  108. def get_image_height(image) -> int:
  109. return len(image)
  110. def get_image_size(image):
  111. height = get_image_height(image)
  112. if height == 0:
  113. return (0,0)
  114. else:
  115. return (get_image_width(image), height)
  116. def put_image(x: int, y: int, image, mode=DM_DRAW, opacity=255):
  117. width, height = get_image_size(image)
  118. if width == 0 or height == 0: return
  119. flattened = ""
  120. for scanline in image:
  121. flattened += "".join([str(color) for color in scanline]) + "00000000" * (width - len(scanline))
  122. command.send("PutImage", X=x, Y=y, Width=width, Height=height, Data=flattened, Mode=mode, Opacity=opacity)
  123. def get_image(x: int, y: int, width: int, height: int):
  124. flattened = command.send("GetImage?", X=x, Y=y, Width=width, Height=height)
  125. str_pos = 0
  126. image = []
  127. for yb in range(0, height):
  128. scanline = []
  129. for xb in range(0, width):
  130. scanline.append(colors.RGBA(int(flattened[str_pos:str_pos + 2],16), int(flattened[str_pos + 2:str_pos + 4],16), int(flattened[str_pos + 4:str_pos + 6],16), int(flattened[str_pos + 6:str_pos + 8],16)))
  131. str_pos = str_pos + 8
  132. image.append(scanline)
  133. return image
  134. def get_pixel(x: int, y: int):
  135. return colors.str_to_RGBA(command.send("GetPixel?", X=x, Y=y))
  136. def fill(color, mode=DM_DRAW):
  137. command.send("LayerFill", Color=color, Mode=mode)
  138. def horizontal_flip():
  139. command.send("LayerHorizontalFlip")
  140. def vertical_flip():
  141. command.send("LayerVerticalFlip")
  142. def clear_alpha(back_color=None):
  143. command.send("ImageClearAlpha", BackColor=back_color)
  144. def fill_background(back_color=None):
  145. command.send("ImageFillBackground", BackColor=back_color)