示例#1
0
def GreyscaleArrayToRgb(array):
    """Converts a greyscale array to RGB array"""
    dimensions = hp.getDimensions(array)
    for h in range(dimensions[0]):
        for w in range(dimensions[1]):
            array[h][w] = hp.greyscaleToRgb(array[h][w])
    return array
示例#2
0
def getCorner(tuple, binary):
    """Returns four pixels of the given corner from (0, 0) in clockwise order."""
    # Point is at the upper-left corner of the pixel with the same coordinate
    y = tuple[0]
    x = tuple[1]
    height, width = hp.getDimensions(binary)
    if y - height > 1 or x - width > 1:
        raise IndexError("Out of index")
    if y < 0 or x < 0:
        raise IndexError("Index must be postive")
    # Clockwise from 0, 0
    deltas = [(-1, -1), (-1, 0), (0, 0), (0, -1)]
    adjacent_pixels = []
    for delta in deltas:
        try:
            new_y = y + delta[0]
            new_x = x + delta[1]
            if new_y < 0 or new_x < 0:
                # Fill in white pixels for out of bound
                pixel = 1
            else:
                # Fill in white pixels for out of bound
                pixel = binary[new_y][new_x]
        except IndexError:
            pixel = 1
        adjacent_pixels.append(pixel)
    return adjacent_pixels
示例#3
0
def saveBitmap(array, filename, binary=False):
    """Saves a new bitmap image file. Debug use only."""
    if binary:
        array = binaryToGreyscale(array)
    dimensions = hp.getDimensions(array)
    array = GreyscaleArrayToRgb(array)
    img = Image.new(getColorMode(array), (dimensions[1], dimensions[0]))
    for h in range(dimensions[0]):
        for w in range(dimensions[1]):
            img.putpixel((w, h), array[h][w])
    img.save(filename)
示例#4
0
def binaryToGreyscale(array):
    """Converts binary value back to 0 or 225"""
    dimensiosns = hp.getDimensions(array)
    binary = []
    for h in range(dimensiosns[0]):
        row = []
        for w in range(dimensiosns[1]):
            if array[h][w] == 1:
                row.append(255)
            else:
                row.append(0)
        binary.append(row)
    return binary
示例#5
0
def decompose(binary):
    """Decompose the binary image into a bunch of closed paths"""
    binary = binary
    dimensions = hp.getDimensions(binary)
    paths = []
    for h in range(dimensions[0] + 1):
        for w in range(dimensions[1] + 1):
            print("Looking for edges at {} \r".format((h, w)), end="")
            if getNextCorner((h, w), binary)[0]:
                edge_path = findEdge((h, w), binary)
                paths.append(edge_path)
                binary = invert(edge_path, binary)
    return paths
示例#6
0
def pythagorean(array1, array2):
    """Returns an array that contains the pythagorean value for each element"""
    dimensions = hp.getDimensions(array1)
    new_array = []
    for h in range(dimensions[0]):
        new_row = []
        for w in range(dimensions[1]):
            value1 = array1[h][w]
            value2 = array2[h][w]
            new_value = (value1**2 + value2**2)**0.5
            new_value = int(round(new_value))
            new_row.append(new_value)
        new_array.append(new_row)
    return new_array
示例#7
0
def applyKernel(array, grey_filter):
    """Apply kernel filter to greyscale array"""
    new_array = []
    if len(grey_filter[0]) % 2 == 0 or len(grey_filter) % 2 == 0:
        raise ValueError
    dimensions = hp.getDimensions(array)
    for h in range(dimensions[0]):
        new_row = []
        for w in range(dimensions[1]):
            diameter = len(grey_filter)
            surroudings = hp.getSurroundingPixels(array, (h, w), diameter)
            applied = hp.applyKernelToLocal(surroudings, grey_filter)
            average = hp.getSum(applied) / len(applied)**2
            average = int(round(average))
            new_row.append(average)
        new_array.append(new_row)
    return new_array
示例#8
0
def getBinary(array, threshold, invert=False):
    dimensiosns = hp.getDimensions(array)
    binary = []
    for h in range(dimensiosns[0]):
        row = []
        for w in range(dimensiosns[1]):
            if array[h][w] > threshold:
                if invert:
                    row.append(0)
                else:
                    row.append(1)
            else:
                if invert:
                    row.append(1)
                else:
                    row.append(0)
        binary.append(row)
    return binary
示例#9
0
def RgbArrayToGreyscale(array, mode="greyscale"):
    """Converts a RGB or RGBA array to greyscale\n
    RGB mode returns pixels as RGB tuples\n
    greyscale mode returns pixels as int"""
    dimensions = hp.getDimensions(array)
    grey_array = []
    for h in range(dimensions[0]):
        row = []
        for w in range(dimensions[1]):
            greyscale = hp.RgbToGreyscale(array[h][w])
            if mode == "RGB":
                grey_rgb = hp.greyscaleToRgb(greyscale)
                row.append(grey_rgb)
            elif mode == "greyscale":
                row.append(greyscale)
            else:
                raise ValueError
        grey_array.append(row)
    return grey_array