def apply_actions_on_image(session_id, action_stack, filename, image_signature): action_stack = deepcopy(action_stack) # If we have arrived to the original image if len(action_stack) == 0: return im_pil0 # Pop out the last action last_action = action_stack.pop() # Apply all the previous action_stack, and gets the image PIL im_pil = apply_actions_on_image(session_id, action_stack, filename, image_signature) im_size = im_pil.size # Apply the rest of the action_stack operation = last_action['operation'] selectedData = last_action['selectedData'] type = last_action['type'] # Select using Lasso if selectedData and 'lassoPoints' in selectedData: selection_mode = 'lasso' selection_zone = generate_lasso_mask(im_pil, selectedData) # Select using rectangular box elif selectedData and 'range' in selectedData: selection_mode = 'select' lower, upper = map(int, selectedData['range']['y']) left, right = map(int, selectedData['range']['x']) # Adjust height difference height = im_size[1] upper = height - upper lower = height - lower selection_zone = (left, upper, right, lower) # Select the whole image else: selection_mode = 'select' selection_zone = (0, 0) + im_size # Apply the filters if type == 'filter': apply_filters(image=im_pil, zone=selection_zone, filter=operation, mode=selection_mode) elif type == 'enhance': enhancement = operation['enhancement'] factor = operation['enhancement_factor'] apply_enhancements(image=im_pil, zone=selection_zone, enhancement=enhancement, enhancement_factor=factor, mode=selection_mode) return im_pil
def apply_actions_on_image(session_id, action_stack, filename, image_signature): action_stack = deepcopy(action_stack) # If we have arrived to the original image if len(action_stack) == 0 and LOCAL: with open("image_string.csv", mode="r") as image_file: image_reader = csv.DictReader(image_file) for row in image_reader: im_pil = drc.b64_to_pil(row["image"]) return im_pil if len(action_stack) == 0 and not LOCAL: # Retrieve the url in which the image string is stored inside s3, # using the session ID url = s3.generate_presigned_url(ClientMethod="get_object", Params={ "Bucket": bucket_name, "Key": session_id }) # A key replacement is required for URL pre-sign in gcp url = url.replace("AWSAccessKeyId", "GoogleAccessId") response = requests.get(url) if DEBUG: print("IMAGE STRING LENGTH: " + str(len(response.text))) im_pil = drc.b64_to_pil(response.text) return im_pil # Pop out the last action last_action = action_stack.pop() # Apply all the previous action_stack recursively, and gets the image PIL im_pil = apply_actions_on_image(session_id, action_stack, filename, image_signature) im_size = im_pil.size # Apply the rest of the action_stack operation = last_action["operation"] selected_data = last_action["selectedData"] action_type = last_action["type"] # Select using Lasso if selected_data and "lassoPoints" in selected_data: selection_mode = "lasso" selection_zone = utils.generate_lasso_mask(im_pil, selected_data) # Select using rectangular box elif selected_data and "range" in selected_data: selection_mode = "select" lower, upper = map(int, selected_data["range"]["y"]) left, right = map(int, selected_data["range"]["x"]) # Adjust height difference height = im_size[1] upper = height - upper lower = height - lower selection_zone = (left, upper, right, lower) # Select the whole image else: selection_mode = "select" selection_zone = (0, 0) + im_size # Apply the filters if action_type == "filter": utils.apply_filters(image=im_pil, zone=selection_zone, filter=operation, mode=selection_mode) elif action_type == "enhance": enhancement = operation["enhancement"] factor = operation["enhancement_factor"] utils.apply_enhancements( image=im_pil, zone=selection_zone, enhancement=enhancement, enhancement_factor=factor, mode=selection_mode, ) return im_pil
def apply_actions_on_image(session_id, action_stack, filename, image_signature): action_stack = deepcopy(action_stack) # If we have arrived to the original image if len(action_stack) == 0: # Retrieve the url in which the image string is stored inside s3, # using the session ID url = s3.generate_presigned_url( ClientMethod='get_object', Params={ 'Bucket': bucket_name, 'Key': session_id } ) # A key replacement is required for URL pre-sign in gcp url = url.replace('AWSAccessKeyId', 'GoogleAccessId') response = requests.get(url) print(len(response.text)) im_pil = drc.b64_to_pil(response.text) return im_pil # Pop out the last action last_action = action_stack.pop() # Apply all the previous action_stack, and gets the image PIL im_pil = apply_actions_on_image( session_id, action_stack, filename, image_signature ) im_size = im_pil.size # Apply the rest of the action_stack operation = last_action['operation'] selectedData = last_action['selectedData'] type = last_action['type'] # Select using Lasso if selectedData and 'lassoPoints' in selectedData: selection_mode = 'lasso' selection_zone = generate_lasso_mask(im_pil, selectedData) # Select using rectangular box elif selectedData and 'range' in selectedData: selection_mode = 'select' lower, upper = map(int, selectedData['range']['y']) left, right = map(int, selectedData['range']['x']) # Adjust height difference height = im_size[1] upper = height - upper lower = height - lower selection_zone = (left, upper, right, lower) # Select the whole image else: selection_mode = 'select' selection_zone = (0, 0) + im_size # Apply the filters if type == 'filter': apply_filters( image=im_pil, zone=selection_zone, filter=operation, mode=selection_mode ) elif type == 'enhance': enhancement = operation['enhancement'] factor = operation['enhancement_factor'] apply_enhancements( image=im_pil, zone=selection_zone, enhancement=enhancement, enhancement_factor=factor, mode=selection_mode ) return im_pil
def apply_actions_on_image(session_id, action_stack, filename, image_signature): action_stack = deepcopy(action_stack) # If we have arrived to the original image if len(action_stack) == 0 and LOCAL: with open("image_string.csv", mode="r") as image_file: image_reader = csv.DictReader(image_file) for row in image_reader: im_pil = drc.b64_to_pil(row["image"]) return im_pil if len(action_stack) == 0 and not LOCAL: pass # Pop out the last action last_action = action_stack.pop() # Apply all the previous action_stack recursively, and gets the image PIL im_pil = apply_actions_on_image(session_id, action_stack, filename, image_signature) im_size = im_pil.size # Apply the rest of the action_stack operation = last_action["operation"] selected_data = last_action["selectedData"] action_type = last_action["type"] # Select using Lasso if selected_data and "lassoPoints" in selected_data: selection_mode = "lasso" selection_zone = utils.generate_lasso_mask(im_pil, selected_data) # Select using rectangular box elif selected_data and "range" in selected_data: selection_mode = "select" lower, upper = map(int, selected_data["range"]["y"]) left, right = map(int, selected_data["range"]["x"]) # Adjust height difference height = im_size[1] upper = height - upper lower = height - lower selection_zone = (left, upper, right, lower) # Select the whole image else: selection_mode = "select" selection_zone = (0, 0) + im_size # Apply the filters if action_type == "filter": utils.apply_filters(image=im_pil, zone=selection_zone, filter=operation, mode=selection_mode) elif action_type == "enhance": enhancement = operation["enhancement"] factor = operation["enhancement_factor"] utils.apply_enhancements( image=im_pil, zone=selection_zone, enhancement=enhancement, enhancement_factor=factor, mode=selection_mode, ) return im_pil
def update_graph_interactive_image(content, n_clicks, figure, selectedData, filters, enhance, enhancement_factor, new_filename, dragmode, enc_format, storage): t1 = time.time() # Retrieve metadata stored in the storage filename = storage # If the file has changed (when a file is uploaded) if new_filename and new_filename != filename: if DEBUG: print(filename, "replaced by", new_filename) string = content.split(';base64,')[-1] im_pil = drc.b64_to_pil(string) # If the file HAS NOT changed (which means an operation was applied) else: # Retrieve the image stored inside the figure enc_str = figure['layout']['images'][0]['source'].split(';base64,')[-1] # Creates the PIL Image object from the b64 png encoding im_pil = drc.b64_to_pil(string=enc_str) im_size = im_pil.size # Select using Lasso if selectedData and 'lassoPoints' in selectedData: selection_mode = 'lasso' selection_zone = generate_lasso_mask(im_pil, selectedData) # Select using rectangular box elif selectedData and 'range' in selectedData: selection_mode = 'select' lower, upper = map(int, selectedData['range']['y']) left, right = map(int, selectedData['range']['x']) # Adjust height difference height = im_size[1] upper = height - upper lower = height - lower selection_zone = (left, upper, right, lower) # Select the whole image else: selection_mode = 'select' selection_zone = (0, 0) + im_size # If the filter dropdown was chosen, apply the filter selected by the user if filters: apply_filters(image=im_pil, zone=selection_zone, filter=filters, mode=selection_mode) if enhance: apply_enhancements(image=im_pil, zone=selection_zone, enhancement=enhance, enhancement_factor=enhancement_factor, mode=selection_mode) t2 = time.time() if DEBUG: print(f"Updated Image Storage in {t2-t1:.3f} sec") return [ drc.InteractiveImagePIL(image_id='interactive-image', image=im_pil, enc_format=enc_format, display_mode='fixed', dragmode=dragmode, verbose=DEBUG), html.Div(id='div-filename-image', children=new_filename, style={'display': 'none'}) ]