def output_test(): width = 256 height = 256 image = ut.create_image(width, height) ut.fill_uv_color(image, width, height) image_name = f'images/{sys._getframe().f_code.co_name}' ut.save_image(image, f'{image_name}.png')
def generate_ridge_data(filename): """Generates a ridge data file from a .nii.gz file Args: filename (str): Filename to evaluate Returns: ndarray: 3-dimensional array of normalized ridge scores """ # extract image data from file background_img = nib.load(filename) background_data = background_img.get_fdata() # generate ridge scores ridge_data = detect_ridges_concurrent(background_data) # save new image base_filename = pattern.search(filename).group(1) ridge_data_filename = f"{base_filename}_ridgeScore.nii.gz" iu.save_image(ridge_data, ridge_data_filename, background_img.affine) # normalize and save normalized data normalized_ridge_data = iu.normalize(ridge_data) normalized_ridge_data_filename = os.path.join( f"{base_filename}_ridgeScore_normalized.nii.gz") iu.save_image(normalized_ridge_data, normalized_ridge_data_filename, background_img.affine) # return normalized ridgescores return ridge_data
def noise_2d_function_test(noise_function, width=256, height=256): image_name = f'images/{noise_function.__name__}' image = np.zeros([height, width]) # Create a noise image for y in range(height): ry = y / (height - 1) for x in range(width): rx = x / (width - 1) uv = np.array([rx, ry]) noise_value = noise_function(uv) assert (0.0 <= noise_value and noise_value <= 1.0), f'{noise_value}' image[y][x] = np.floor(noise_value * 256) # Get its histogram hist, bins = np.histogram(image.ravel(), 256, [0, 256]) plt.cla() plt.clf() plt.step(bins[:-1], hist, where='post') plt.savefig(f'{image_name}_hist.png') # Get its FFT image image_color = ut.convert_monochrome_to_color(image, width, height) image_fft2 = ut.get_fft2_image(image) image_fft2 = ut.convert_monochrome_to_color(image_fft2, width, height) # Save images ut.save_image(image_color, f'{image_name}.png') ut.save_image(image_fft2, f'{image_name}_fft.png')
def export_grid(result_dir, batch_cnt, grid, name): batch, height, width, depth, channel = grid.shape for b in range(batch): for d in range(depth): save_image( np.squeeze(grid[b, :, :, d, :]), os.path.join(result_dir, name + '_%d_%d_%d.png' % (batch_cnt, b, d)), 'RGB')
def to_gray_and_save_pic(uri, elaborated_folder): image = image_utils.open_image(uri, "RGB") if not image: return False else: if image.format == "PNG": image = image.convert('LA') else: image = image.convert('L') image_utils.save_image(image, basename(uri), elaborated_folder) return True
def pic_in_tiles(uri, thumbs_folder, mosaic_folder, mosaic_tile_size, num_clusters, ratio, use_images): """ use_images: if True, each tile is replaced with an image else with dominant color """ normalized_image = image_utils.enlarge_crop_img(uri, mosaic_tile_size, ratio) if not normalized_image: # img is too small return False target_image = image_utils.create_image("RGB", normalized_image.size, "white") grid_crops_original_image = image_utils.subdivide_img(normalized_image, mosaic_tile_size) rgb_matrix_dataset = dataset_thumbs.load_dataset_in_matrix() core.find_dominant_color_and_replace(grid_crops_original_image, target_image, num_clusters, rgb_matrix_dataset, thumbs_folder, mosaic_tile_size, use_images) image_utils.save_image(target_image, basename(uri), mosaic_folder) return True
def to_sepia_and_save_pic(uri, elaborated_folder): """ Convert before in grayscale and then in sepia """ img = image_utils.open_image(uri, "RGB") if img: orig_mode = img.mode if orig_mode != "L": img = img.convert("L") sepia = make_linear_ramp() img.putpalette(sepia) img = img.convert(orig_mode) image_utils.save_image(img, basename(uri), elaborated_folder) return True else: return False
def preprocess_and_save_images(self): # saves all the images (prepared for the model) os.makedirs(self.preprocessing_path, exist_ok=True) if ('whiten_imgs' in self.img_settings['img_preprocessing']): imgs = whiten_all_images(self.data_path, self.df["img_name"].values.tolist(), self.img_settings["img_channels"]) else: imgs = self.process_images() for i, img_name in enumerate(self.df['img_name']): # save image after pre-processing save_image(imgs[i], "{}/{}".format(self.preprocessing_path, img_name))
def dream(image_filename, layer): """ image_filename is the path to the image to process layer is a dictionnary, containing the layer name and the feature channels to maximise. It has the following format: { name: 'conv2d0:0', fromChannel: 0, toChannel: 10 } """ global model start_loading = time.time() model = inception5h.Inception5h() session = tf.compat.v1.Session(graph=model.graph) image = image_utils.load_image(filename=image_filename) start_processing = time.time() # The image we're going to be working on should be within 400x600 pixels for performance reasons. DESIRED_WIDTH = 400 DESIRED_HEIGHT = 600 rescale_factor = min(DESIRED_WIDTH / image.shape[1], DESIRED_HEIGHT / image.shape[0]) image_downscaled = image_utils.resize_image(image=image, factor=rescale_factor) layer_tensor = model.get_layer_tensor_by_name( layer['name'])[:, :, :, layer['fromChannel']:layer['toChannel'] + 1] print('Layer tensor: ' + str(layer_tensor)) img_result = recursive_optimize(layer_tensor=layer_tensor, image=image_downscaled, num_iterations=10, step_size=3.0, num_repeats=4, blend=0.2, session=session) upscaled_result = image_utils.resize_image(image=img_result, size=image.shape) image_utils.save_image(upscaled_result, filename='img/deapdream_image.jpg') session.close() end_processing = time.time() print( "Loading time: %f sec, processing time: %f sec" % (start_processing - start_loading, end_processing - start_processing))
def on_key_press(self, key): if key == 63234: # left print "left by 100 px" elif key == 63235: # right print "right by 100 px" elif key == 108: # l print "laser on" self.laser_service.on() elif key == 111: # o print "laser off" self.laser_service.off() elif key == 32: # space print "range find!" def image_capture_fn(): frame = self.video.capture_image(self.video.camera) img.save_image(frame, "rangefind.jpg") return frame # distance = self.range_finder.compute_distance(image_capture_fn) # print "Distance to target: %s" % distance elif key == 97: # a frame = self.video.current_frame img.save_image(frame, "frame.jpg") elif key == 114: # r # draw rect image = self.video.current_frame rows = image.shape[0] cols = image.shape[1] top_left = ((cols / 2 - 1) - 10, rows / 2 - 1) bottom_right = ((cols / 2 - 1) + 10, rows - 1) self.video.add_rectangle(top_left, bottom_right) # self.range_finder.extract_laser_channel_rect(image) elif key == 102: # f frame = self.video.current_frame circles = self.range_finder.find_red_dot_circles(frame) print "circles:" for circle in circles: print circle self.video.add_click_point(circle) # print "coords: (x=%s, y=%s)" % coords elif key == 13: # enter print "fire gun!" return 0
def save_results(self, epoch, sample_a, sample_b): ### save result img file exp_config = "D_{}_G_{}_ngf_{}_ndf_{}_lambda_{}_norm_{}".format( self.args.modelD, self.args.modelG, self.args.ngf, self.args.ndf, self.args.lamb, self.args.norm_type) result_dir = os.path.join(self.args.save_dir, 'logs', self.args.model, self.args.dataset, exp_config) if not os.path.exists(result_dir): os.makedirs(result_dir) fake_filename = os.path.join(result_dir, 'epoch%03d.png' % (epoch + 1)) # self.G.eval() self.set_requires_grad(self.G, False) if self.is_cuda == True: sample_a = sample_a.cuda() sample_b = sample_b.cuda() fake_b = self.G.forward(sample_a) results = torch.cat((sample_a, fake_b, sample_b), 3) results_img = tensor2img(results) save_image(results_img, fake_filename)
def pic_in_tiles(uri, thumbs_folder, mosaic_folder, mosaic_tile_size, num_clusters, ratio, use_images): """ use_images: if True, each tile is replaced with an image else with dominant color """ normalized_image = image_utils.enlarge_crop_img(uri, mosaic_tile_size, ratio) if not normalized_image: # img is too small return False target_image = image_utils.create_image("RGB", normalized_image.size, "white") grid_crops_original_image = image_utils.subdivide_img( normalized_image, mosaic_tile_size) rgb_matrix_dataset = dataset_thumbs.load_dataset_in_matrix() core.find_dominant_color_and_replace(grid_crops_original_image, target_image, num_clusters, rgb_matrix_dataset, thumbs_folder, mosaic_tile_size, use_images) image_utils.save_image(target_image, basename(uri), mosaic_folder) return True
def classify_image(images, mask_list, k_size, save, display): """ Classify pixels of a single image """ if len(images) > 1: raise ValueError('Only one image can be classified at once') logging.info('Calculating, normalizing feature vectors for image') image = images[0] # First and only member vectors = calculate_features(image.image, image.fov_mask, mask_list, k_size) logging.info('Classifying image pixels') probabilities, prediction = svm.classify(vectors) svm.assess(image.truth, prediction) svm.plot_roc(image.truth, probabilities) if save: image_utils.save_image(prediction, 'prediction.png') logging.info('Saved classified image') if display: image_utils.display_image(prediction) logging.info('Displaying classified image')
def train(self, iterations=100000, minibatch_size=32): # initialize session sess = tf.Session() sess.run(tf.global_variables_initializer()) data_sampler = MNISTDataSampler() for iteration in range(iterations): # train discriminator real_samples = data_sampler.get_random_images(minibatch_size) random_noise = self.generate_random_noise(minibatch_size) _, D_loss = sess.run([self.D_optimizer, self.D_loss], feed_dict={ self.X: real_samples, self.Z: random_noise }) # train generator for _ in range(1): random_noise = self.generate_random_noise(minibatch_size) _, G_loss, G_output = sess.run( [ self.G_optimizer, self.G_loss, self.G_output, ], feed_dict={self.Z: random_noise}) if iteration % 100 == 0: print( "Iteration {0}: discriminator loss = {1}, generator loss = {2}" .format(iteration + 1, str(round(D_loss, 3)), str(round(G_loss, 3)))) if iteration % 1000 == 0: save_image(G_output[0], "visualization", "output_iter-{0}.png".format(iteration + 1))
def __init__(self, corners: geometry_utils.Polygon, horizontal_cells: int, vertical_cells: int, image: np.ndarray, save_path: typing.Optional[pathlib.PurePath] = None): """Initiate a new Grid. Corners should be clockwise starting from the top left - if not, the grid will have unexpected behavior. If `save_path` is provided, will save the resulting image to this location as "grid.jpg". Used for debugging purposes.""" self.corners = corners self.horizontal_cells = horizontal_cells self.vertical_cells = vertical_cells self._to_grid_basis, self._from_grid_basis = geometry_utils.create_change_of_basis( corners[0], corners[3], corners[2]) self.horizontal_cell_size = 1 / self.horizontal_cells self.vertical_cell_size = 1 / self.vertical_cells self.image = image if save_path: image_utils.save_image(save_path / "grid.jpg", self.draw_grid())
def find_dominant_color_and_generate_thumb(image, pic, thumbs_folder, num_clusters, thumb_size, save_thumb): """ Find dominant color of a picture and returns color If save_thumb is True than create and save thumbnail with the num_clusters common colors """ image = resize_image(image, (thumb_size, thumb_size)) ar, shape, codes, vecs = calculate_dominant_colors(image, num_clusters) dominant_color = calculate_dominant_color_from_clusters(codes, vecs) if save_thumb: im = convert_scipy_image_to_n_colors(ar, shape, codes, vecs) if save_image(im, pic, thumbs_folder): return dominant_color, im else: return None, None else: return dominant_color, None
def find_dominant_color_and_generate_thumb(image, pic, thumbs_folder, num_clusters, thumb_size, save_thumb): """ Find dominant color of a picture and returns color If save_thumb is True than create and save thumbnail with the num_clusters common colors """ image = resize_image(image, (thumb_size, thumb_size)) ar, shape, codes, vecs = calculate_dominant_colors(image, num_clusters) dominant_color = calculate_dominant_color_from_clusters(codes, vecs) if save_thumb: im = convert_scipy_image_to_n_colors(ar, shape, codes, vecs) if save_image(im, pic, thumbs_folder): return dominant_color, im else: return None, None else: return dominant_color, None
def log_images(self, x, epoch, name_suffix, name, channels=3, nrow=8): img_path = os.path.join(self.experiment_name, name) os.makedirs(img_path, exist_ok=True) img_size = self.im_size if img_size < 1: img_size2 = x.nelement() / x.size(0) / channels img_size = int(np.sqrt(img_size2)) x = x.view(-1, channels, img_size, img_size) # * 0.5 + 0.5 grid = save_image(x, img_path + '/sample_' + str(epoch) + "_" + str(name_suffix) + '.jpg', nrow=nrow, normalize=True, scale_each=True) img_grid = make_grid(x, normalize=True, scale_each=True, nrow=nrow) self.writer.add_image(name, img_grid, self.iter_global) return
def image_capture_fn(): frame = self.video.capture_image(self.video.camera) img.save_image(frame, "rangefind.jpg") return frame
grid_1, grid_2, grid_3, batch_loss = sess.run( [guide_net['guide_1'], guide_net['guide_2'], guide_net['guide_3'], \ guide_net['weight_1'], guide_net['weight_2'], guide_net['denoised_hdr'],\ guide_net['from_grid_hdr_1'], guide_net['from_grid_hdr_2'], guide_net['from_grid_hdr_3'], guide_net['grid_1'],guide_net['grid_2'],guide_net['grid_3'], guide_net['loss_all_L1']], feed_dict, options=run_options, run_metadata=run_metadata) tl = timeline.Timeline(run_metadata.step_stats) ctf = tl.generate_chrome_trace_format(show_memory=True) with open(os.path.join(errorlog_dir, 'timeline.json'), 'w') as wd: wd.write(ctf) for k in range(0, src_hdr.shape[0]): idx_all = batch_cnt * test_batch_size + k save_image(tone_mapping(src_hdr[k, :, :, 0:3]), os.path.join(result_dir, '%d_src.png' % idx_all), 'RGB') save_image(tone_mapping(tgt_hdr[k, :, :, :]), os.path.join(result_dir, '%d_tgt.png' % idx_all), 'RGB') save_image(tone_mapping(denoised_hdr[k, :, :, :]), os.path.join(result_dir, '%d_rcn.png' % idx_all), 'RGB') if args.export_exr: save_exr(src_hdr[k, :, :, 0:3], os.path.join(result_dir, '%d_src.exr' % idx_all)) save_exr(tgt_hdr[k, :, :, :], os.path.join(result_dir, '%d_tgt.exr' % idx_all)) save_exr(denoised_hdr[k, :, :, :], os.path.join(result_dir, '%d_rcn.exr' % idx_all))
from extract_data import extract_extended_ck_data from image_utils import save_image x, y = extract_extended_ck_data() for i in range(len(x)): path = f'data/own/{i}_{y[i]}.png' save_image(x[i], path)
activations_G, x) cache.grads = grads return J def df(x): assert cache.grads is not None grads = cache.grads cache.grads = None # fmin_l_bfgs_b only handles 1D arrays grads = grads.flatten() # fmin_l_bfgs_b needs this to be float64 for some undocumented weird reason grads = grads.astype(np.float64) return grads # It only occurred to me mid way through this project that I would not be able to use # Keras's built in optimizers, as the loss function needs to be supplied explicitly, # hence why using scipy's L-BFGS optimizer here. # The optimizer will find pixel values that lower the scalar output of the cost function 'f' generated_image, min_val, info = fmin_l_bfgs_b(f, generated_image.flatten(), fprime=df, maxfun=40) generated_image = generated_image.reshape(input_shape) image_utils.save_image('output/output_%d.png' % (time.time()), generated_image) #plt.imshow(image_utils.convert(generated_image)) plt.plot(np.arange(0, len(J_history)), J_history, label='J')
def resize_and_save_pic(uri, width, height, elaborated_folder): image = image_utils.open_image(uri, "RGB") image = image_utils.resize_image(image, (width,height)) image_utils.save_image(image, basename(uri), elaborated_folder) return True
from_grid_hdr_1, from_grid_hdr_2, from_grid_hdr_3, \ grid_1, grid_2, grid_3, batch_loss = sess.run( [guide_net['guide_1'], guide_net['guide_2'], guide_net['guide_3'], \ guide_net['weight_1'], guide_net['weight_2'], guide_net['denoised_hdr'],\ guide_net['from_grid_hdr_1'], guide_net['from_grid_hdr_2'], guide_net['from_grid_hdr_3'], guide_net['grid_1'],guide_net['grid_2'],guide_net['grid_3'], loss_all_L1], feed_dict) denoised = tone_mapping(denoised_hdr) tgt = tone_mapping(tgt_hdr) _, batch_psnr_val = batch_psnr(denoised, tgt) epoch_avg_psnr_valid += batch_psnr_val epoch_avg_loss_valid += batch_loss if batch_cnt in VALID_DISPLAY_LIST: for k in range(tgt.shape[0]): save_image(tgt[k, :, :, :], os.path.join(result_dir, 'e%d_b%d_i%d_tgt.png'%(epoch_i, batch_cnt, k)), 'RGB') save_image(denoised[k, :, :, :], os.path.join(result_dir, 'e%d_b%d_i%d_rcn.png'%(epoch_i, batch_cnt, k)), 'RGB') save_image(tone_mapping(src_hdr[k,:,:,0:3]), os.path.join(result_dir, 'e%d_b%d_i%d_src.png'%(epoch_i, batch_cnt, k)), 'RGB') if args.export_grid_output: save_image(tone_mapping(from_grid_hdr_1[k,:,:,:]), os.path.join(result_dir, 'e%d_b%d_i%d_from_grid_1.png'%(epoch_i, batch_cnt, k)), 'RGB') save_image(tone_mapping(from_grid_hdr_2[k,:,:,:]), os.path.join(result_dir, 'e%d_b%d_i%d_from_grid_1.png'%(epoch_i, batch_cnt, k)), 'RGB') save_image(tone_mapping(from_grid_hdr_3[k,:,:,:]), os.path.join(result_dir, 'e%d_b%d_i%d_from_grid_1.png'%(epoch_i, batch_cnt, k)), 'RGB') if args.export_guide_weight: save_image(guide_1[k, :, :], os.path.join(result_dir, 'e%d_b%d_i%d_guide_1.png'%(epoch_i, batch_cnt, k)))
noisy_3_tm = tone_mapping(noisy_3) denoised_1_tm = tone_mapping(denoised_hdr_1) denoised_2_tm = tone_mapping(denoised_hdr_2) denoised_3_tm = tone_mapping(denoised_hdr_3) tgt = tone_mapping(tgt_hdr) src = tone_mapping(src_hdr[:,:,:,0:3]) _, batch_psnr_val = batch_psnr(denoised_1_bd_tm, tgt) epoch_avg_psnr_valid += batch_psnr_val epoch_avg_loss_valid += batch_loss if batch_cnt in VALID_DISPLAY_LIST: for k in range(tgt.shape[0]): save_image(tgt[k, :, :, :], os.path.join(result_dir, 'e%d_b%d_i%d_tgt.png'%(epoch_i, batch_cnt, k)), 'RGB') save_image(denoised_1_bd_tm[k, :, :, :], os.path.join(result_dir, 'e%d_b%d_i%d_rcn.png'%(epoch_i, batch_cnt, k)), 'RGB') save_image(src[k,:,:,:], os.path.join(result_dir, 'e%d_b%d_i%d_src.png'%(epoch_i, batch_cnt, k)), 'RGB') if args.export_all: save_image(noisy_1_tm[k, :, :, :], os.path.join(result_dir, 'e%d_b%d_i%d_full_noisy.png'%(epoch_i, batch_cnt, k))) save_image(noisy_2_tm[k, :, :, :], os.path.join(result_dir, 'e%d_b%d_i%d_half_noisy.png'%(epoch_i, batch_cnt, k))) save_image(noisy_3_tm[k, :, :, :], os.path.join(result_dir, 'e%d_b%d_i%d_quat_noisy.png'%(epoch_i, batch_cnt, k))) save_image(denoised_1_tm[k,:, :], os.path.join(result_dir, 'e%d_b%d_i%d_full_denoised.png'%(epoch_i, batch_cnt, k)))
def process(): input = request.json image_data = re.sub('^data:image/.+;base64,', '', input['img']) image_ascii = save_image(image_data) return image_ascii
(x_points, y_points) = points (width, height) = IMAGES_SIZE image_size_x = width * len(x_points) image_size_y = height * len(y_points) image = np.zeros((image_size_y, image_size_x, 3)) for step_x, x in enumerate(x_points): for step_y, y in enumerate(y_points): [decoded] = decoder.predict(np.array([[x, y]])) pixel_x = step_x * width pixel_y = step_y * height image[pixel_y:pixel_y + height, pixel_x:pixel_x + width] = decoded return image if __name__ == "__main__": encoder = load_model(f"{TMP_DIR}/encoder_13.h5", compile=False) decoder = load_model(f"{TMP_DIR}/decoder_13.h5", compile=False) images = load_images() predicted_positions = encoder.predict(images) points = create_a_map_of_even_separated_points(predicted_positions) image = build_latent_space_map(points, decoder) filepath = f"{GENERATED_IMAGES_DIR}/latent_map.png" save_image(image, filepath) print(f"saved at {filepath}")
def save_images(images, size, image_path): return iu.save_image(images, size, image_path)
denoised_2_bd_tm = tone_mapping(denoised_2_bd) denoised_3_bd_tm = tone_mapping(denoised_3_bd) if args.export_all: noisy_1_tm = tone_mapping(noisy_1) noisy_2_tm = tone_mapping(noisy_2) noisy_3_tm = tone_mapping(noisy_3) denoised_1_tm = tone_mapping(denoised_hdr_1) denoised_2_tm = tone_mapping(denoised_hdr_2) denoised_3_tm = tone_mapping(denoised_hdr_3) for k in range(0, src_hdr.shape[0]): idx_all = batch_cnt * test_batch_size + k save_image(tgt[k, :, :, :], os.path.join(result_dir, '%d_tgt.png' % idx_all), 'RGB') save_image(src[k, :, :, :], os.path.join(result_dir, '%d_src.png' % idx_all), 'RGB') save_image(denoised_1_bd_tm[k, 0:IMAGE_HEIGHT, :, :], os.path.join(result_dir, '%d_rcn.png' % idx_all), 'RGB') if args.export_exr: save_exr(src_hdr_in[k, :, :, :], os.path.join(result_dir, '%d_src.exr' % idx_all)) save_exr(tgt_hdr_in[k, :, :, :], os.path.join(result_dir, '%d_tgt.exr' % idx_all)) save_exr(denoised_1_bd[k, 0:IMAGE_HEIGHT, :, :], os.path.join(result_dir, '%d_rcn.exr' % idx_all))