image.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. from lazpaint import command, dialog, colors, layer
  2. if __name__ == "__main__":
  3. dialog.show_message("Library to act on the whole image.")
  4. RESAMPLE_QUALITY_BOX = 'Box'
  5. RESAMPLE_QUALITY_LINEAR = 'Linear'
  6. RESAMPLE_QUALITY_HALF_COSINE = 'HalfCosine'
  7. RESAMPLE_QUALITY_COSINE = 'Cosine'
  8. RESAMPLE_QUALITY_BICUBIC = 'Bicubic'
  9. RESAMPLE_QUALITY_MITCHELL = 'Mitchell'
  10. RESAMPLE_QUALITY_SPLINE = 'Spline'
  11. RESAMPLE_QUALITY_LANCZOS2 = 'Lanczos2'
  12. RESAMPLE_QUALITY_LANCZOS3 = 'Lanczos3'
  13. RESAMPLE_QUALITY_LANCZOS4 = 'Lanczos4'
  14. RESAMPLE_QUALITY_BEST = 'BestQuality'
  15. ANCHOR_TOP_LEFT = 'TopLeft'
  16. ANCHOR_TOP = 'Top'
  17. ANCHOR_TOP_RIGHT = 'TopRight'
  18. ANCHOR_LEFT = 'Left'
  19. ANCHOR_MIDDLE = 'Middle'
  20. ANCHOR_RIGHT = 'Right'
  21. ANCHOR_BOTTOM_LEFT = 'BottomLeft'
  22. ANCHOR_BOTTOM = 'Bottom'
  23. ANCHOR_BOTTOM_RIGHT = 'BottomRight'
  24. def new(width: int, height: int, color=colors.TRANSPARENT, ignore_modified=False):
  25. command.send("FileNew", Width=width, Height=height, BackColor=color, IgnoreModified=ignore_modified)
  26. def paste_as_new():
  27. command.send("EditPasteAsNew")
  28. def get_width() -> int:
  29. return command.send("GetImageWidth?")
  30. def get_height() -> int:
  31. return command.send("GetImageHeight?")
  32. def get_size() -> tuple:
  33. return command.send("GetImageSize?")
  34. def get_registry(identifier):
  35. str_result = command.send("ImageGetRegistry?", Identifier=identifier)
  36. if str_result == "":
  37. return None
  38. else:
  39. return command.parse_str(str_result)
  40. def set_registry(identifier, value):
  41. if value == None:
  42. value = ""
  43. else:
  44. value = str(value)
  45. command.send("ImageSetRegistry", Identifier=identifier, Value=value)
  46. def get_layer_index(layer_id=None) -> int:
  47. return command.send("GetLayerIndex?", LayerId=layer_id)
  48. def iterate_layers():
  49. prev_id = layer.get_id()
  50. for layer_id in get_all_layers_id():
  51. layer.select_id(layer_id)
  52. yield layer_id
  53. layer.select_id(prev_id)
  54. def get_all_layers_id() -> list:
  55. return command.send("GetAllLayersId?")
  56. def contains_layer_id(layer_id) -> bool:
  57. return get_layer_index(layer_id) is not None
  58. def select_layer_index(index: int): #1..layer_count
  59. return command.send("SelectLayerIndex", Index=index)
  60. def move_layer_index(from_index: int, to_index: int):
  61. return command.send("ImageMoveLayerIndex", FromIndex=from_index, ToIndex=to_index)
  62. def get_layer_count() -> int:
  63. return command.send("GetLayerCount?")
  64. def get_frame_index() -> int: #1..frame_count
  65. return command.send("GetFrameIndex?")
  66. def get_frame_count() -> int:
  67. return command.send("GetFrameCount?")
  68. def load_frame(frame_index=None, ignore_modified=False) -> int:
  69. return command.send("FileChooseEntry?", EntryIndex=frame_index, IgnoreModified=ignore_modified)
  70. def new_frame(width=None, height=None, back_color=colors.TRANSPARENT, ignore_modified=False) -> int:
  71. command.send("FileNewEntry", Width=width, Height=height, BackColor=back_color, IgnoreModified=ignore_modified)
  72. def open(file_name=None, ignore_modified=False):
  73. command.send("FileOpen", FileName=file_name, IgnoreModified=ignore_modified)
  74. def save(skip_options=False) -> str:
  75. return command.send("FileSave?", SkipOptions=skip_options)
  76. def save_as(file_name=None, validate=False, overwrite=False, skip_options=False) -> str:
  77. return command.send("FileSaveAs?", FileName=file_name, Validate=validate, Overwrite=overwrite, SkipOptions=skip_options)
  78. def export(file_name=None, validate=False, overwrite=False, skip_options=False) -> str:
  79. return command.send("FileSaveAs?", FileName=file_name, Validate=validate, Overwrite=overwrite, SkipOptions=skip_options, Export=True)
  80. def change_file_extension(file_name: str, new_extension: str) -> str:
  81. base = file_name.rsplit('.', 1)[0]
  82. if len(new_extension) > 0 and new_extension[0:1] != ".":
  83. new_extension = "." + new_extension
  84. return base + new_extension
  85. def get_temporary_name() -> str:
  86. return command.send("FileGetTemporaryName?")
  87. def reload(ignore_modified=False):
  88. command.send("FileReload", IgnoreModified=ignore_modified)
  89. def get_name() -> str:
  90. return command.send("GetFileName?")
  91. def resample(width: int, height: int, quality=RESAMPLE_QUALITY_BEST):
  92. command.send("ImageResample", Width=width, Height=height, Quality=quality, Validate=True)
  93. def smart_zoom3():
  94. command.send("ImageSmartZoom3")
  95. def horizontal_flip():
  96. command.send("ImageHorizontalFlip")
  97. def vertical_flip():
  98. command.send("ImageVerticalFlip")
  99. def rotate_cw():
  100. command.send("ImageRotateCW")
  101. def rotate_ccw():
  102. command.send("ImageRotateCCW")
  103. def negative():
  104. command.send("ImageNegative")
  105. def linear_negative():
  106. command.send("ImageLinearNegative")
  107. def swap_red_blue():
  108. command.send("ImageSwapRedBlue")
  109. def crop_to_selection():
  110. command.send("ImageCrop")
  111. def crop_to_selection_and_layer():
  112. command.send("ImageCropLayer")
  113. def flatten():
  114. command.send("ImageFlatten")
  115. def canvas_size(width: int, height: int, anchor=ANCHOR_MIDDLE):
  116. command.send("ImageCanvasSize", Width=width, Height=height, Anchor=anchor)
  117. def repeat(width: int, height: int, anchor=ANCHOR_MIDDLE, flip=False):
  118. command.send("ImageRepeat", Width=width, Height=height, Anchor=anchor, Flip=flip)
  119. def undo():
  120. command.send("EditUndo")
  121. def redo():
  122. command.send("EditRedo")
  123. # starts a series of actions (undoable with only one call to "undo")
  124. def do_begin():
  125. command.send("EditDoBegin")
  126. # returns True if some action was done within the series of actions
  127. def do_end() -> bool:
  128. return command.send("EditDoEnd?")