def scale(input_img, size):
    """Scale image to given size.

    The input must be a grey image.
    Usage:
        output_img = scale(input_img, size)
    """

    if size == input_img.size:
        return create_image(input_img.mode, size,
                            lambda x, y: input_img.getpixel((x, y)))

    # calculate measures of input/output
    in_size = input_img.size
    in_width, in_height = in_size
    out_width, out_height = size

    # relative horizontal coordinates of output images
    x = arange(0.0, in_width, float(in_width) / out_width)
    # relative vertical coordinates of output images
    y = arange(0.0, in_height, float(in_height) / out_height)

    # get interpolated value
    im = ImageForProcess(input_img)
    ip = im.get_interpolation()
    pixels = ip(y, x)

    return create_image(input_img.mode, size, lambda x, y: pixels[y][x])
def scale(input_img, size):
    """Scale image to given size.

    The input must be a grey image.
    Usage:
        output_img = scale(input_img, size)
    """

    if size == input_img.size:
        return create_image(
            input_img.mode, size,
            lambda x, y: input_img.getpixel((x, y)))

    # calculate measures of input/output
    in_size = input_img.size
    in_width, in_height = in_size
    out_width, out_height = size

    # relative horizontal coordinates of output images
    x = arange(0.0, in_width, float(in_width)/out_width)
    # relative vertical coordinates of output images
    y = arange(0.0, in_height, float(in_height)/out_height)

    # get interpolated value
    im = ImageForProcess(input_img)
    ip = im.get_interpolation()
    pixels = ip(y, x)

    return create_image(input_img.mode, size, lambda x, y: pixels[y][x])
Beispiel #3
0
 def create_disks(self):
    """create root and swap images"""
    #check if image already exists, if it does use it
    print "Creating disk image %s\n" % self._path
    if not os.path.isfile(self._path):
        if not util.create_image(self._path, self._size):
             
            print"Formatting %s as %s" % (self._path, self._fs)
            if util.format_fs(self._path, self._fs) :
                print "%s not supported\nExiting" % self._fs
        else:    
            print "Could not create disk image\nExiting"
            return 1
    else:
        print "%s exists, using existing image\n" % self._path
     
    #check if swap image exists, if it does use it
    print "Creating swap image %s\n" % self._swap
    if not os.path.isfile(self._swap):
        if not util.create_image(self._swap, self._swapsize, "M"):
            util.format_fs(self._swap, "swap")
        else:    
            print "Could not create swap image\nExiting"
            return 1
    else:
        print "%s exists, using existing image\n" % self._swap
Beispiel #4
0
    def create_disks(self):
        """create root and swap images"""
        #check if image already exists, if it does use it
        print "Creating disk image %s\n" % self._path
        if not os.path.isfile(self._path):
            if not util.create_image(self._path, self._size):

                print "Formatting %s as %s" % (self._path, self._fs)
                if util.format_fs(self._path, self._fs):
                    print "%s not supported\nExiting" % self._fs
            else:
                print "Could not create disk image\nExiting"
                return 1
        else:
            print "%s exists, using existing image\n" % self._path

        #check if swap image exists, if it does use it
        print "Creating swap image %s\n" % self._swap
        if not os.path.isfile(self._swap):
            if not util.create_image(self._swap, self._swapsize, "M"):
                util.format_fs(self._swap, "swap")
            else:
                print "Could not create swap image\nExiting"
                return 1
        else:
            print "%s exists, using existing image\n" % self._swap
def quantize(input_img, level):
    """Quantize image for given grey level.

    The input must be a grey image.
    Usage:
        output_img = quantize(input_img, level)
    """
    im = ImageForProcess(input_img)
    pixels = im.get_pixels()

    if len(input_img.getcolors()) == level:
        return create_image(
            input_img.mode,
            input_img.size,
            lambda x, y: pixels[y][x])

    # new color palette
    palette = [255 * i / (level - 1) for i in range(level)]

    find_neighbor = NeighborFinder(palette)
    lookup = map(find_neighbor, range(256))

    return create_image(
        input_img.mode,
        input_img.size,
        lambda x, y: lookup[pixels[y][x]])
def solve(input_path):
    f = open(input_path)
    tiles = util.parse(f)

    grid = construct_grid(tiles)

    # Now the values of tiles have been flipped and/or rotated
    # and grid is a 2-dimensional array indexed as [x][y].
    for k, v in tiles.iteritems():
        tiles[k] = util.strip_border(v)

    # Next make 8 versions of string lists.
    images = [None] * 8
    images[0] = util.create_image(tiles, grid)
    images[1] = util.text_rotate(images[0])
    images[2] = util.text_rotate(images[1])
    images[3] = util.text_rotate(images[2])
    images[4] = util.text_flip(images[3])
    images[5] = util.text_rotate(images[4])
    images[6] = util.text_rotate(images[5])
    images[7] = util.text_rotate(images[6])

    for i in range(len(images)):
        if util.highlight_monsters(images[i]):
            return sum(
                [sum([1 for c in row if c == '#']) for row in images[i]])
Beispiel #7
0
def quantize(input_img, level):
    """Quantize image for given grey level.

    The input must be a grey image.
    Usage:
        output_img = quantize(input_img, level)
    """
    im = ImageForProcess(input_img)
    pixels = im.get_pixels()

    if len(input_img.getcolors()) == level:
        return create_image(input_img.mode, input_img.size,
                            lambda x, y: pixels[y][x])

    # new color palette
    palette = [255 * i / (level - 1) for i in range(level)]

    find_neighbor = NeighborFinder(palette)
    lookup = map(find_neighbor, range(256))

    return create_image(input_img.mode, input_img.size,
                        lambda x, y: lookup[pixels[y][x]])
def test_view_as_window(filename, result_dir):
    input_img = Image.open(filename)

    cases = [((96, 64), (20605, 36769, 52239, 4993, 16885, 16036, 6120, 2692)),
             ((50, 50), (21668, 11292, 44273, 51172, 53769, 67841, 5296,
                         53054))]
    for patch_size, keys in cases:
        windows = view_as_window(input_img, patch_size)
        for i, key in enumerate(keys):
            patch = windows[key]
            result = create_image(input_img.mode, patch_size,
                                  lambda x, y: patch[y][x])
            result_name = 'patch-%d-%d-%d.png' % (patch_size[0], patch_size[1],
                                                  i)
            result_path = os.path.join(result_dir, result_name)
            result.save(result_path)
            print '[Saved] ' + result_path
def test_view_as_window(filename, result_dir):
    input_img = Image.open(filename)

    cases = [((96, 64),
              (20605, 36769, 52239, 4993, 16885, 16036, 6120, 2692)),
             ((50, 50),
              (21668, 11292, 44273, 51172, 53769, 67841, 5296, 53054))]
    for patch_size, keys in cases:
        windows = view_as_window(input_img, patch_size)
        for i, key in enumerate(keys):
            patch = windows[key]
            result = create_image(input_img.mode,
                                  patch_size,
                                  lambda x, y: patch[y][x])
            result_name = 'patch-%d-%d-%d.png' % (patch_size[0],
                                                  patch_size[1], i)
            result_path = os.path.join(result_dir, result_name)
            result.save(result_path)
            print '[Saved] ' + result_path
Beispiel #10
0
def filter2d(input_img, filter):
    """Apply 2-d filter to a 2-d image."""
    N, M = input_img.size  # M is height, N is width
    m, n = len(filter), len(filter[0])  # m is height, n is width
    a, b = m / 2, n / 2
    patches = view_as_window(input_img, (n, m))
    pixels = patches.pixels
    # get transpose of the 1-d filter
    if (isinstance(filter, np.ndarray)):
        wt = filter.flatten()
    else:
        wt = np.array(filter).flatten()

    def correlation(x, y):
        # z = np.zeros(n * m)  # pad with zeros
        z = np.full(n * m, pixels[y][x])  # pad with border duplicates
        # fill in available neighborhood
        for j in range(y - a, y + a + 1):
            for i in range(x - b, x + b + 1):
                if i > 0 and i < N and j > 0 and j < M:
                    z[(j - y + a) * n + i - x + b] = pixels[j][i]
        return np.dot(wt, z)

    return create_image(input_img.mode, input_img.size, correlation)