Example #1
0
def scalar_multiplication(first_image: ImageWrapper, scalar: float):
    w, h = first_image.dimensions()

    result = ImageWrapper.from_dimensions(w, h, mode=first_image.get_mode())

    for x in range(w):
        for y in range(h):
            val = [
                int(first_image.get_pixel(x, y)[i] * scalar) % 255
                for i in range(len(first_image.get_pixel(x, y)))
            ]
            result.set_pixel(x, y, tuple(val))

    return result
Example #2
0
def pixel_transformation(image: ImageWrapper, transformation_function):
    w, h = image.dimensions()

    matrix = []

    for x in range(w):
        row = []
        for y in range(h):
            val = transformation_function(x, y, image.get_pixel(x, y))

            row.append(tuple(val))
        matrix.append(row)

    return matrix_to_image(normalized_matrix(matrix), mode=image.get_mode())
Example #3
0
def diffusion_step(c_calculator: Callable[[float], float], image: ImageWrapper,
                   step: float) -> ImageWrapper:
    w, h = image.dimensions()
    new_image: ImageWrapper = ImageWrapper.from_dimensions(
        w, h, image.get_mode())

    new_channels = []
    for channel in image.channels:
        new_channel = np.zeros(shape=(w, h))
        for x in range(w):
            for y in range(h):
                new_channel[x, y] = channel[x, y] + step * (
                    calculate_direction(x, y, channel, 'N', c_calculator) +
                    calculate_direction(x, y, channel, 'S', c_calculator) +
                    calculate_direction(x, y, channel, 'E', c_calculator) +
                    calculate_direction(x, y, channel, 'O', c_calculator))
        new_channels.append(new_channel)

    new_image.channels = new_channels
    new_image.draw_image()
    return new_image
Example #4
0
def bilateral_filter(image: ImageWrapper, window_dim: Tuple[int, int],
                     sigma_s: float, sigma_r: float) -> ImageWrapper:
    window_w, window_h = window_dim
    if window_w % 2 == 0 or window_h % 2 == 0:
        raise ValueError("Window dimensions should be odd numbers")

    w, h = image.dimensions()
    new_image: ImageWrapper = ImageWrapper.from_dimensions(
        w, h, image.get_mode())

    new_channels = [np.zeros(shape=(w, h))] * len(image.channels)
    for y in range(h):
        for x in range(w):
            values = calculate_filter(image.channels, x, y, window_dim,
                                      sigma_s, sigma_r)
            for val, channel in zip(values, new_channels):
                channel[x, y] = val

    new_image.channels = new_channels
    new_image.draw_image()
    return new_image
Example #5
0
def pixel_to_pixel_operation(first_image: ImageWrapper,
                             second_image: ImageWrapper, operation):
    assert first_image.dimensions() == second_image.dimensions()

    w, h = first_image.dimensions()
    matrix = []

    for x in range(w):
        row = []
        for y in range(h):
            val = [
                operation(
                    first_image.get_pixel(x, y)[i],
                    second_image.get_pixel(x, y)[i])
                for i in range(len(first_image.get_pixel(x, y)))
            ]
            row.append(tuple(val))
        matrix.append(row)

    return matrix_to_image(normalized_matrix(matrix),
                           mode=first_image.get_mode())