def SelectionTool(Canvas, window_title):
    while True:
        print()
        print("Enter 'O' for Object Selection Tool.")
        print("Enter 'Q' for Quick Selection Tool.")
        print("Enter 'M' for Magic Wand Tool.")

        command = input("\nEnter command: ")
        command = command.replace(" ", "")

        if len(command) > 1:  # If more than 1 command entered.
            print("Too many commands entered. Enter only one command.\n")
            continue

        elif 'o' in command or 'O' in command:  # 'o'/'O' entered -> Object Selection Tool
            selection.ObjectSelectionTool(Canvas, window_title)
            break

        elif 'q' in command or 'Q' in command:  # 'q'/'Q' entered -> Quick Selection Tool
            selection.QuickSelectionTool(Canvas, window_title)
            break

        elif 'm' in command or 'M' in command:  # 'm'/'M' entered -> Magic Wand Tool
            selection.MagicWandTool(Canvas, window_title)
            break

        else:  # If invalid command is passed.
            print("Invalid command passed. Enter command again.\n")
            continue

    hf.Sleep()

    return
def LassoTool(Canvas, window_title):
    while True:
        print()
        print("Enter 'L' for Lasso Tool.")
        print("Enter 'P' for Polygon Lasso Tool.")
        print("Enter 'M' for Magnetic Lasso Tool.")

        command = input("\nEnter command: ")
        command = command.replace(" ", "")

        if len(command) > 1:  # If more than 1 command entered.
            print("Too many commands entered. Enter only one command.\n")
            continue

        elif 'l' in command or 'L' in command:  # 'l'/'L' entered -> Lasso Tool
            lasso.LassoTool(Canvas, window_title)
            break

        elif 'p' in command or 'P' in command:  # 'p'/'P' entered -> Polygon Lasso Tool
            lasso.PolygonLassoTool(Canvas, window_title)
            break

        elif 'm' in command or 'M' in command:  # 'm'/'M' entered -> Magnetic Lasso Tool
            lasso.MagneticLassoTool(Canvas, window_title)
            break

        else:  # If invalid command is passed.
            print("Invalid command passed. Enter command again.\n")
            continue

    hf.Sleep()

    return
def TakeOperation_Num(MaxNum):
    # Taking input
    Operation_Num = input("\nEnter the operation number: ")

    try:  # If valid number passed
        Operation_Num = Operation_Num.replace(" ", "")
        Operation_Num = int(Operation_Num)
        if 0 <= Operation_Num < MaxNum:
            return Operation_Num
        else:
            raise ValueError
    except:  # If invalid number passed
        print("\nInvalid operation number entered.")
        hf.Sleep()
        return None
def ChooseLayersToShow(Canvas, window_title):
    # Checking boundary condition
    if len(Canvas.layers) == 0:  # If no layer is present
        print("\nYou should have atleast 1 layer to show/hide layers.\n")
        return

    print("\nEnter the layer numbers (comma(',') separated) you wish to see.")
    print("-1 for all layers and -2 for no layer.\n")
    Canvas.PrintLayerNames()

    # Creating a copy of Canvas to work on
    Canvas_copy = Canvas.Copy()

    IsAborted = False
    while True:
        Canvas_copy.Show(Title=window_title)
        key = cv2.waitKey(1)

        command = input(
            "\nEnter 'Y/N' to confirm/abort arrangement else enter any other key to select different set of layers: "
        )

        if 'Y' in command or 'y' in command:  # 'Y' or 'y' is pressed. - confirm
            break
        elif 'N' in command or 'n' in command:  # 'N' or 'n' is pressed. - abort
            IsAborted = True
            break

        # Asking for layer nos and checking if valid umbers passed
        layer_nos = AskForLayerNumbers(-2, len(Canvas_copy.layers) - 1)
        if layer_nos is None:  # layer_nos = None if invalid layer nos entered
            continue

        # Setting layers' visibility as asked
        Canvas_copy.SetLayersVisibility(layer_nos)

    if IsAborted:
        print("\nChoosing of layers aborted.\n")

    else:
        print("\nChoosing of layers successful.\n")
        Canvas_copy.Copy(copyTo=Canvas)

    cv2.destroyWindow(window_title)

    hf.Sleep()

    return
def LayerOperations(Canvas, window_title):
    while True:
        print()
        print("Enter 'R' to rearrange layers.")
        print("Enter 'D' to delete layers.")
        print("Enter 'M' to merge layers.")
        print("Enter 'E' to rename layers.")
        print("Enter 'C' to duplicate layers.")

        command = input("\nEnter command: ")
        command = command.replace(" ", "")

        if len(command) > 1:  # If more than 1 command entered.
            print("Too many commands entered. Enter only one command.\n")
            continue

        elif 'r' in command or 'R' in command:  # 'r'/'R' entered -> Rearrange layers
            RearrangeLayers(Canvas, window_title)
            break

        elif 'd' in command or 'D' in command:  # 'd'/'D' entered -> Delete layers
            DeleteLayers(Canvas, window_title)
            break

        elif 'm' in command or 'M' in command:  # 'm'/'M' entered -> Merge layers
            MergeLayers(Canvas, window_title)
            break

        elif 'e' in command or 'E' in command:  # 'e'/'E' entered -> Rename layers
            RenameLayers(Canvas)
            break

        elif 'c' in command or 'C' in command:  # 'c'/'C' entered -> Duplicate layer
            DuplicateLayers(Canvas)
            break

        else:  # If invalid command is passed.
            print("Invalid command passed. Enter command again.\n")
            continue

    cv2.destroyWindow(window_title)
    hf.Sleep()

    return
def MarqueeTool(Canvas, window_title):
    while True:
        print()
        print("Enter 'R' for Rectangular Marquee Tool.")
        print("Enter 'E' for Elliptical Marquee Tool.")
        print("Enter 'W' for Single Row Marquee Tool.")
        print("Enter 'C' for Single Column Marquee Tool.")

        command = input("\nEnter command: ")
        command = command.replace(" ", "")

        if len(command) > 1:  # If more than 1 command entered.
            print("Too many commands entered. Enter only one command.\n")
            continue

        elif 'r' in command or 'R' in command:  # 'r'/'R' entered -> Rectangular Marquee Tool
            marquee.RectangularMarqueeTool(Canvas, window_title)
            break

        elif 'e' in command or 'E' in command:  # 'e'/'E' entered -> Elliptical Marquee Tool
            marquee.EllipticalMarqueeTool(Canvas, window_title)
            break

        elif 'w' in command or 'W' in command:  # 'w'/'W' entered -> Single Row Marquee Tool
            marquee.SingleRowMarqueeTool(Canvas, window_title)
            break

        elif 'c' in command or 'C' in command:  # 'c'/'C' entered -> Single Column Marquee Tool
            marquee.SingleColMarqueeTool(Canvas, window_title)
            break

        else:  # If invalid command is passed.
            print("Invalid command passed. Enter command again.\n")
            continue

    hf.Sleep()

    return
def AddNewLayer(Canvas):
    print("\nEnter '@' to abort adding of new image.")

    while True:
        NewImagePath = input("\nEnter image path: ")

        if '@' in NewImagePath:  # Abort if '@' entered
            print("Aborting adding of image.\n")
            break

        # Reading new image
        NewImage = image.ReadImage(NewImagePath)
        if NewImage is None:  # If image is not found/read
            print("Enter a valid image path...")
            continue

        # Calling function for adding new layer
        Canvas.AddLayer(NewImage)
        print("\nLayer added successfully.")

        break

    hf.Sleep()