def zoom_out(image_name, step): image = Image.open(image_name) width = range(0, image.size[0], step) height = range(0, image.size[1], step) image_data = [] new_x = 0 new_y = 0 for x in width: new_y = 0 for y in height: quadrants = [(x, y), (x + 1, y), (x, y + 1), (x + 1, y + 1)] int_rgb_total = 0 for quadrant in quadrants: if x <= image.size[0] and y <= image.size[1]: pixel = image.getpixel((x,y)) int_rgb_total += utils.getIfromRGB(pixel) else: quadrants.remove(quadrant) new_pixel_int = round(int_rgb_total / len(quadrants)) new_pixel = utils.getRGBfromI(int(new_pixel_int)) image_data.append({ 'coordinates': (new_x, new_y), 'pixel': new_pixel }) new_y = new_y + 1 new_x = new_x + 1 utils.create_new_image('zoom_out.jpg', image_data)
def convolution(image_name, kernel, new_image): image = Image.open(image_name) width = range(0, image.size[0]) height = range(0, image.size[1]) image_data = [] offset_x = (kernel.get('width', 0) - 1) / 2 offset_y = (kernel.get('height', 0) - 1) / 2 for x in width: for y in height: conv_r = 0 conv_g = 0 conv_b = 0 for kx in range(0, kernel.get('width', 0)): x_coord = x + kx - offset_x if x + kx < offset_x or x_coord >= image.size[0]: continue for ky in range(0, kernel.get('height', 0)): y_coord = y + ky - offset_y if y + ky < offset_y or y_coord >= image.size[1]: continue kernel_pixel = kernel.get('matrix', [])[kx][ky] image_pixel = image.getpixel((x_coord, y_coord)) conv_r += (image_pixel[0] * kernel_pixel) conv_g += (image_pixel[1] * kernel_pixel) conv_b += (image_pixel[2] * kernel_pixel) if conv_r > 255: conv_r = 255 if conv_r < 0: conv_r = 0 if conv_g > 255: conv_g = 255 if conv_g < 0: conv_g = 0 if conv_b > 255: conv_b = 255 if conv_b < 0: conv_b = 0 image_data.append({ 'coordinates': (x, y), 'pixel': (conv_r, conv_g, conv_b) }) utils.create_new_image('{}.jpg'.format(new_image), image_data)
if FLAGS.image_path == FLAGS.image_output_path or FLAGS.video_path == FLAGS.video_output_path: raise Exception('Input and output directories cannot be the same') yolo = prepare_detector() p2p = Pix2Pix(mode='try', checkpoint_dir='pix2pix/checkpoint/') if FLAGS.image_path: input_folder = FLAGS.image_path for file in os.listdir(input_folder): input = input_folder + file image = read_image(input) image_yolo = prepare_image_yolo(image) output_yolo = yolo(image_yolo) output_yolo = cut_result(output_yolo) final_image = create_new_image(image, output_yolo, p2p, FLAGS.objects) if os.path.isfile(FLAGS.image_output_path + '/' + file): os.remove(FLAGS.image_output_path + '/' + file) plt.imsave(f'{FLAGS.image_output_path}/{file}', final_image * 0.5 + 0.5) elif FLAGS.video_path: print(FLAGS.video_path) height, width = None, None writer = None try: vid = cv2.VideoCapture(FLAGS.video_path) except: raise Exception('Video cannot be loaded!\n\ Please check the path provided!') finally: