def fourier_effect(img, steps): f_img = np.fft.rfft2(img, axes=(0, 1)) new_f_img = f_img y_max = f_img.shape[0] x_max = f_img.shape[1] print("") for y in range(0, y_max): tools.display_percentage("running Fourier stuff... ", y, y_max) for x in range(0, x_max): new_f_img[y, x, 0] = ( f_img[y, x][0].real * sqrt(abs(x - (x_max / 2))) + f_img[y, x][0].imag ) new_f_img[y, x, 1] = ( f_img[y, x][1].real * sqrt(abs(x - (x_max / 2))) + f_img[y, x][1].imag ) new_f_img[y, x, 2] = ( f_img[y, x][2].real * sqrt(abs(x - (x_max / 2))) + f_img[y, x][2].imag ) for index, step in enumerate(steps): if x % step == 0: new_f_img[y][x][index] = 0 new_img = np.real(np.fft.irfft2(new_f_img, axes=(0, 1))) return new_img
def spread_primary_colours(img: np.array, mapping_array: np.array) -> np.array: for x in range(0, 3): mapping_array[x] = np.array(mapping_array[x]) * 1.0 / np.sum( mapping_array[x]) for i, row in enumerate(img): tools.display_percentage("running spread_primary_colours... ", i, img.shape[0]) for j, element in enumerate(row): img[i, j] = mapping_array.dot(img[i, j]) return np.array(img) * 255.0 / np.max(img)
def wavify_circle( img: np.array, line_count: int, thickness: int, overlap: int ) -> np.array: args = locals() del args["img"] print("args: ", args) new_img = np.zeros(img.shape) if img.shape[2] == 4: new_img[:, :, 3] = img[:, :, 3] x_bar = int(img.shape[0] / 2) y_bar = int(img.shape[1] / 2) sparseness = int((x_bar + y_bar) / line_count) pixel_thickness = thickness * sparseness / 100 if sparseness < 1: raise ValueError( "ratio of img res to line_count is too low, resulting sparseness = 0" ) for x, row in enumerate(img): display_percentage("running wavify_circle... ", x, img.shape[0]) for y, px in enumerate(row): if on_concentric_circle(x, y, x_bar, y_bar, sparseness, pixel_thickness): split_colours = True if split_colours: new_x, new_y = new_coords( x, y, x_bar, y_bar, px[0], sparseness, overlap ) new_img[new_x, new_y, 0] = px[0] new_x, new_y = new_coords( x, y, x_bar, y_bar, px[1], sparseness, overlap ) new_img[new_x, new_y, 1] = px[1] new_x, new_y = new_coords( x, y, x_bar, y_bar, px[2], sparseness, overlap ) new_img[new_x, new_y, 2] = px[2] else: px_bar = np.average(px) new_x, new_y = new_coords( x, y, x_bar, y_bar, px_bar, sparseness, overlap ) new_img[new_x, new_y, 0] = px[0] new_img[new_x, new_y, 1] = px[1] new_img[new_x, new_y, 2] = px[2] return new_img
def create_kernel(img_shape, gaussian_accent, process): x_ = int(img_shape[0] / 2 + 1) y_ = int(img_shape[1] / 2 + 1) kernel = np.zeros(img_shape) for xIndex, x in enumerate(kernel): tools.display_percentage("running %s blur... " % process, xIndex, img_shape[0]) for yIndex, y in enumerate(x): kernel[xIndex, yIndex] = gaussian_2d( xIndex, yIndex, x_, y_, gaussian_accent ) return kernel
def mixup(img: np.array, weight: float, cutoff: int) -> np.array: y_max = img.shape[0] x_max = img.shape[1] new_img = img for y in range(0, y_max): tools.display_percentage("running mixup... ", y, y_max) for x in range(0, x_max): if img[y, x, 2] < cutoff and img[y, x, 1] < cutoff: new_x = int(((x + img[y, x, 2]) * 2) % x_max) new_img[y, new_x][:] = (1.0 - weight) * img[y, x][:] + weight * img[ y, new_x ][:] new_img[y, x][:] = ( weight * img[y, x][:] + (1.0 - weight) * img[y, new_x][:] ) return new_img
def wavify_monochrome( img, sparseness, thickness, overlap, offset, constant_wave, constant_wave_count ) -> np.array: print("") img = img.astype(np.float) h_line_shape = img.shape[1] blank_h_line = np.zeros(h_line_shape) line_img = np.array( [sparsify(x, sparseness, i, blank_h_line, offset) for i, x in enumerate(img)] ) wavy_img = np.zeros(line_img.shape) line_img = line_img * 1.0 / np.max(line_img) max_index = [wavy_img.shape[0] - 1, wavy_img.shape[1] - 1] for i, row in enumerate(line_img): display_percentage("running wavify... ", i, img.shape[0]) if (i - offset) % sparseness != 0: continue for j, element in enumerate(row): if not constant_wave: new_y_index = wrap( max_index[0], (i + verticalise(sparseness, element, overlap)) ) else: wavelength = int(1.0 * max_index[1] / constant_wave_count) new_y_index = wrap( max_index[0], (i + sin_verticalise(sparseness, element, overlap, j, wavelength)), ) for x in range(0, thickness): wavy_img[wrap(max_index[0], new_y_index + x), j] = 255 return wavy_img
def affect_on_line_contrast(img, contrast=10, span=10, vertical=False, randomise=False, less_than=True): args = locals() del args["img"] print("args: ", args) if vertical: img = np.swapaxes(img, 0, 1) new_img = np.asarray(img[:]) y_max = img.shape[0] - 1 x_max = img.shape[1] - 1 for y in range(0, y_max): tools.display_percentage("running outlines... ", y, y_max) for x in range(span, x_max - span): max_val = 0 min_val = 255 avg = 0 for i in range(x - span, x + span): if max(img[y, i, 0:2]) > max_val: max_val = max(img[y, i, 0:2]) elif min(img[y, i, 0:2]) < min_val: min_val = min(img[y, i, 0:2]) avg += img[y, i, 0] avg /= span * 2 + 1 if less_than == ((max_val - min_val) < contrast): if randomise: rand_y = int(y - (span / 2) + random.random() * span) new_red = get_rand_colour_from_y_span( img, rand_y, x, y_max, 0) new_green = get_rand_colour_from_y_span( img, rand_y, x, y_max, 1) new_blue = get_rand_colour_from_y_span( img, rand_y, x, y_max, 2) else: new_red = int(1.0 * (int(img[wrap(y_max, y), wrap(x_max, x - 1), 0]) + int(img[wrap(y_max, y), wrap(x_max - 1, x - 2), 0]) + int(img[wrap(y_max, y), wrap(x_max, x + 1), 0]) + int(img[wrap(y_max, y), wrap(x_max, x + 2), 0])) / 4) new_green = int(1.0 * (int(img[wrap(y_max, y), wrap(x_max, x - 1), 1]) + int(img[wrap(y_max, y), wrap(x_max - 1, x - 2), 1]) + int(img[wrap(y_max, y), wrap(x_max, x + 1), 1]) + int(img[wrap(y_max, y), wrap(x_max, x + 2), 1])) / 4) new_blue = int(1.0 * (int(img[wrap(y_max, y), wrap(x_max, x - 1), 2]) + int(img[wrap(y_max, y), wrap(x_max - 1, x - 2), 2]) + int(img[wrap(y_max, y), wrap(x_max, x + 1), 2]) + int(img[wrap(y_max, y), wrap(x_max, x + 2), 2])) / 4) if new_red > 10 or new_green > 10 or new_blue > 10: if img.shape[2] == 4: new_img[y][max(x - span, 0):min(x + span, x_max)][:] = [ new_red, new_green, new_blue, 255, ] else: new_img[y][max(x - span, 0):min(x + span, x_max)][:] = [ new_red, new_green, new_blue, ] if vertical: new_img = np.swapaxes(new_img, 0, 1) return new_img
def linify( img, separate_colours=False, line_factor=4, lean=0, allow_line_merging=False, left_only=False, straight_only=False, ): if line_factor == 0: line_factor = 1 args = locals() del args["img"] print("args: ", args) new_img = np.zeros(img.shape) colour_map = {0: "red", 1: "blue", 2: "green", 3: "???"} number_of_lines = int(500 / line_factor) - 1 gradient_array = np.zeros((img.shape[0], img.shape[1] - 1, img.shape[2])) img_temp = img[:, 1:, :] gradient_array = img[:, :-1, :] - img_temp img_width = img.shape[1] - 2 for colour in range(0, 2): offset = colour if separate_colours else (line_factor / 2) previous_positions = np.zeros((img.shape[0])) previous_positions = [ x * line_factor + 1 for x in range(0, number_of_lines) ] new_img[0, 2::line_factor, :] = img[0, 2::line_factor, :] for i in range(1, img.shape[0]): # lean = -lean display_percentage( "running linify for colour %s... " % colour_map[colour], i, img.shape[0]) for j in range(0, number_of_lines): direction = 0 if left_only: if (gradient_array[i, wrap(img_width - 1, previous_positions[j]), colour] - gradient_array[i, wrap(img_width - 1, previous_positions[j] - 1), colour] > lean): direction = 1 else: if straight_only: gradient_direction = gradient_array[ i, wrap(img_width - 1, previous_positions[j]), colour] - int( gradient_array[i, wrap(img_width - 1, previous_positions[j] - 1), colour, ]) if gradient_direction > lean: direction = 1 elif gradient_direction < lean: direction = -1 else: direction = int( gradient_array[i, wrap(img_width - 1, previous_positions[j]), colour] - int(gradient_array[i, wrap(img_width - 1, previous_positions[j] - 1), colour, ]) / 500) if separate_colours: new_img[i, wrap(img_width, previous_positions[j] + direction), colour] = img[ i, wrap(img_width, previous_positions[j] + direction), colour] else: if straight_only: new_img[ i, wrap(img_width, previous_positions[j] + direction), 0] = img[i, wrap(img_width, previous_positions[j] + direction), 0] new_img[ i, wrap(img_width, previous_positions[j] + direction), 1] = img[i, wrap(img_width, previous_positions[j] + direction), 1] new_img[ i, wrap(img_width, previous_positions[j] + direction), 2] = img[i, wrap(img_width, previous_positions[j] + direction), 2] else: new_img[i, previous_positions[j]:wrap( img_width, previous_positions[j] + direction), 0, ] = img[ i, wrap(img_width, previous_positions[j] + direction), 0] new_img[i, previous_positions[j]:wrap( img_width, previous_positions[j] + direction), 1, ] = img[ i, wrap(img_width, previous_positions[j] + direction), 1] new_img[i, previous_positions[j]:wrap( img_width, previous_positions[j] + direction), 2, ] = img[ i, wrap(img_width, previous_positions[j] + direction), 2] if (allow_line_merging or previous_positions[wrap( number_of_lines - 1, j + 1)] > previous_positions[j]): previous_positions[j] += direction previous_positions[j] = wrap(img_width, previous_positions[j]) return new_img