示例#1
0
def runProgram(image: np.ndarray,
               PS: programState) -> Union[programState, BaseException]:
    """
    Executes all steps from the image
    :param image: input image
    :param PS: current program state with which to make the next step
    :return: Either the last program state, or a runtime exception
    """
    newState = copy.deepcopy(PS)

    if colors.isBlack(imageWrapper.getPixel(image, newState.position)):
        return errors.inBlackPixelError(
            "Programstate starts in black pixel at {}".format(
                newState.position))

    currentCodel = imageWrapper.getCodel(image, newState.position)
    newGraph = newState.graph.graph
    graphNode = newGraph[currentCodel]
    newToken = graphNode.graphNode[newState.direction][0]

    if isinstance(newToken, tokens.terminateToken):
        return newState

    newState = takeStep(image, newState)
    if isinstance(newState, BaseException):
        return newState

    return runProgram(image, newState)
示例#2
0
    def updateEdgesInfo(self, image, inputGraph, programState):
        edgesInfo = self.builder.get_object('codelEdgesMessage',
                                            self.generalInfo)

        if colors.isBlack(imageWrapper.getPixel(image, programState.position)):
            edgesInfo.configure(
                text="Black pixels are no codel, and have no edges")
            return None

        codel = imageWrapper.getCodel(image, programState.position)
        baseString = "Next step will be:\n"

        graphNode = inputGraph.graph[codel]

        edge = graphNode.graphNode[programState.direction]
        baseString += self.getEdgeDescription(edge, programState.direction)

        baseString += "\nCodel edges are as follows:\n"
        #Generate pointers
        edgePointers = list(
            map(lambda i: direction((i % 4, int(i / 4))), iter(range(8))))
        for edgePointer in edgePointers:
            edge = graphNode.graphNode[edgePointer]
            baseString += self.getEdgeDescription(edge, edgePointer)
        edgesInfo.configure(text=baseString)
示例#3
0
 def unHighlightCodel(self):
     codel = imageWrapper.getCodel(self.image,
                                   self.previousProgramState.position)
     pixel = imageWrapper.getPixel(self.image,
                                   self.previousProgramState.position)
     color = self.pixelToHexString(pixel)
     self.colorCodel(codel, color, color)
示例#4
0
 def highlightCodel(self):
     """
     Outlines the current codel with complemented colors
     :return:
     """
     codel = imageWrapper.getCodel(self.image, self.programState.position)
     pixel = imageWrapper.getPixel(self.image, self.programState.position)
     color = self.pixelToHexString(pixel)
     outline = self.pixelToHexString(
         self.complement(int(pixel[0]), int(pixel[1]), int(pixel[2])))
     self.colorCodel(codel, color, outline)
示例#5
0
def edgeToToken(
        image: np.ndarray,
        inputEdge: edge) -> Union[tokens.baseLexerToken, BaseException]:
    """
    This function creates a token based on the given edge
    :param image: input image
    :param inputEdge: an edge containing (coords, direction)
    :return: Either a newly created token, or an exception
    """
    if not imageWrapper.boundsChecker(image, inputEdge.edge[0]):
        return IndexError("Edge position {} is not in image".format(
            inputEdge.edge[0]))

    nextPosition = movement.getNextPosition(inputEdge.edge[0],
                                            inputEdge.edge[1].pointers[0])
    if not imageWrapper.boundsChecker(image, nextPosition):
        return tokens.toBlackToken("edge")

    pixel = imageWrapper.getPixel(image, nextPosition)

    if colors.isBlack(pixel):
        return tokens.toBlackToken("toBlack")

    if colors.isWhite(pixel):
        return tokens.toWhiteToken()

    if not colors.isColor(pixel):
        return tokens.toBlackToken("Unknown color")

    colorChange = colors.getPixelChange(
        imageWrapper.getPixel(image, inputEdge.edge[0]),
        imageWrapper.getPixel(image, nextPosition))
    if isinstance(colorChange, BaseException):
        # Modify existing error message with location
        newText = "{} at position {}".format(colorChange.args[0], nextPosition)
        return errors.UnknownColorError(newText)

    tokenType = tokens.getTokenType(colorChange['hueChange'],
                                    colorChange['lightChange'])
    return tokens.toColorToken(
        tokenType, len(imageWrapper.getCodel(image, inputEdge.edge[0]).codel))
示例#6
0
def graphImage(image: np.ndarray) -> Tuple[graph, List[BaseException]]:
    """
    Returns a dict with hashes of each codel as keys, and a codelDict as value. That codelDict contains hashed pointers (Tuple[int, int]) as keys to tokens as values.
    :param image:
    :return:
    """
    coords = np.ndindex(image.shape[1], image.shape[0])
    # Converts tuples of coordinates into position objects
    positions = map(position, coords)
    # Makes a list of non-black pixel positions
    nonBlackPositions = list(
        filter(
            lambda pos: not colors.isBlack(imageWrapper.getPixel(image, pos)),
            positions))
    # Gets all codels from all non-black pixel positions
    allCodels = getCodels(image, nonBlackPositions)
    # Makes a graph with the codel as key, and the node as value
    return codelsToGraph(image, allCodels)
示例#7
0
def getCodels(image: np.ndarray, positionList: List[position]) -> List[codel]:
    """
    Makes a list of codels from an image and a lits of positions to check
    :param image: an np.ndarray representing the image
    :param positionList: A list of positions, for which to find adjacent pixels of the same color
    :return: A list of codels found in the given image
    """
    if len(positionList) == 0:
        return []

    copiedList = positionList.copy()
    newPosition = copiedList.pop(0)

    if colors.isBlack(imageWrapper.getPixel(image, newPosition)):
        return getCodels(image, copiedList)

    newCodel = imageWrapper.getCodel(image, newPosition)

    # Remove found positions from coords list
    copiedList = list(set(copiedList) - newCodel.codel)
    codelList = getCodels(image, copiedList)

    codelList.append(newCodel)
    return codelList