def run_my_task(self, inputs, settings, outputs): task_image = load_image( inputs['Greyscale or one-bit PNG image'][0]['resource_path']) staff_finder = StaffFinder_miyao(task_image, 0, 0) fn_kwargs = { 'num_lines': settings['Number of lines'], 'scanlines': settings['Scan lines'], 'blackness': settings['Blackness'], 'tolerance': settings['Tolerance'], } staff_finder.find_staves(**fn_kwargs) poly_list = staff_finder.get_polygon() poly_list = fix_poly_point_list(poly_list, staff_finder.staffspace_height) poly_json_list = create_polygon_outer_points_json_dict(poly_list) # If poly output, save it if 'Polygons (Miyao results)' in outputs: poly_string = str(poly_json_list) f = open(outputs['Polygons (Miyao results)'][0]['resource_path'], 'w') f.write(poly_string) f.close() # If image output, save it if 'PNG image (Miyao results used as mask)' in outputs: mask_img = Image.new('L', (task_image.ncols, task_image.nrows), color='white') mask_drawer = ImageDraw.Draw(mask_img) for polygon in poly_json_list: flattened_poly = [j for i in polygon for j in i] mask_drawer.polygon(flattened_poly, outline='black', fill='black') del mask_drawer task_image_rgb = task_image.to_rgb( ) #Because gamera masking doesn't work on onebit or grey16 images. segment_mask = from_pil(mask_img).to_onebit() result_image_rgb = task_image_rgb.mask(segment_mask) result_image = ensure_pixel_type( result_image_rgb, outputs['PNG image (Miyao results used as mask)'][0] ['resource_type']) for i in range( len(outputs['PNG image (Miyao results used as mask)'])): result_image.save_PNG( outputs['PNG image (Miyao results used as mask)'][i] ['resource_path'])
def run_my_task(self, inputs, settings, outputs): if '@polygon_outer_points' not in settings: task_image = load_image(inputs['PNG image'][0]['resource_path']) polygon_outer_points = '[]' if 'Polygons' in inputs: with open(inputs['Polygons'][0]['resource_path'], 'r') as myfile: polygon_outer_points = myfile.read().replace('\n', '') settings_update = { '@image_width': task_image.ncols, '@polygon_outer_points': polygon_outer_points } return self.WAITING_FOR_INPUT(settings_update) else: polygon_outer_points = settings['@polygon_outer_points'] # If image output port, write image if 'PNG image (masked)' in outputs: task_image = load_image( inputs['PNG image'][0]['resource_path']) mask_img = Image.new('L', (task_image.ncols, task_image.nrows), color='white') mask_drawer = ImageDraw.Draw(mask_img) for polygon in polygon_outer_points: flattened_poly = [j for i in polygon for j in i] mask_drawer.polygon(flattened_poly, outline='black', fill='black') del mask_drawer task_image_rgb = task_image.to_rgb( ) # Because gamera masking doesn't work on onebit or grey16 images. segment_mask = from_pil(mask_img).to_onebit() result_image_rgb = task_image_rgb.mask(segment_mask) result_image = ensure_pixel_type( result_image_rgb, outputs['PNG image (masked)'][0]['resource_type']) result_image.save_PNG( outputs['PNG image (masked)'][0]['resource_path']) # If polygons image output port, write poly file if 'Polygons' in outputs: poly_string = str(polygon_outer_points) f = open(outputs['Polygons'][0]['resource_path'], 'w') f.write(poly_string) f.close()
def process_image(self, task_image, settings): mask_img = Image.new('L', (task_image.ncols, task_image.nrows), color='white') mask_drawer = ImageDraw.Draw(mask_img) try: polygon_data = json.loads(settings['polygon_outer_points']) except ValueError: # There's a problem in the JSON - it may be malformed, or empty polygon_data = [] for polygon in polygon_data: flattened_poly = [j for i in polygon for j in i] mask_drawer.polygon(flattened_poly, outline='black', fill='black') del mask_drawer task_image_greyscale = task_image.to_greyscale() # Because gamera masking doesn't work on one-bit images segment_mask = from_pil(mask_img).to_onebit() result_image_greyscale = task_image_greyscale.mask(segment_mask) result_image = result_image_greyscale.to_onebit() # Get it back to one-bit return result_image
def process_image(self, task_image, settings): # Get the returned data. try: geometries = json.loads(settings['geometries']) except ValueError: geometries = [] # Make copy of original imageOriginal = task_image.to_pil() # For each of the geometries provided, we have to apply the pixel colour to non-white pixels. for geometry in geometries: imageOriginal = self._apply_geometry(imageOriginal, geometry) # Convert red to white and black to green. colour_swap = {(255, 0, 0): (255, 255, 255), (0, 255, 0): (0, 0, 0)} imageOriginal = self._colour_swap_pixels(imageOriginal, colour_swap) # Convert back to gamera image and return. finalImage = from_pil(imageOriginal) return finalImage
def create_mask(org_img, ulx=0, uly=0, lrx=0, lry=0, imw=0): if 0 == imw: scale_factor = 1 else: scale_factor = float(org_img.ncols) / float(imw) arg_ulx = scale_factor * ulx arg_uly = scale_factor * uly arg_lrx = scale_factor * lrx - 1 arg_lry = scale_factor * lry - 1 if arg_ulx < 0: arg_ulx = 0 if arg_ulx > org_img.ncols: arg_ulx = org_img.ncols if arg_lrx < 0: arg_lrx = 0 if arg_lrx > (org_img.ncols - 1): arg_lrx = org_img.ncols - 1 if arg_uly < 0: arg_uly = 0 if arg_uly > org_img.nrows: arg_uly = org_img.nrows if arg_lry < 0: arg_lry = 0 if arg_lry > (org_img.nrows - 1): arg_lry = org_img.nrows - 1 im = Image.new('L', (org_img.ncols, org_img.nrows), color='white') draw = ImageDraw.Draw(im) draw.rectangle((arg_ulx, arg_uly, arg_lrx, arg_lry), fill='black') del draw im = from_pil(im) return im.to_onebit()
def run_my_task(self, inputs, settings, outputs): if '@polygon_outer_points' not in settings: task_image = load_image(inputs['PNG image'][0]['resource_path']) polygon_outer_points = '[]' if 'Polygons' in inputs: with open(inputs['Polygons'][0]['resource_path'], 'r') as myfile: polygon_outer_points = myfile.read().replace('\n', '') settings_update = {'@image_width': task_image.ncols, '@polygon_outer_points': polygon_outer_points} return self.WAITING_FOR_INPUT(settings_update) else: polygon_outer_points = settings['@polygon_outer_points'] # If image output port, write image if 'PNG image (masked)' in outputs: task_image = load_image(inputs['PNG image'][0]['resource_path']) mask_img = Image.new('L', (task_image.ncols, task_image.nrows), color='white') mask_drawer = ImageDraw.Draw(mask_img) for polygon in polygon_outer_points: flattened_poly = [j for i in polygon for j in i] mask_drawer.polygon(flattened_poly, outline='black', fill='black') del mask_drawer task_image_rgb = task_image.to_rgb() # Because gamera masking doesn't work on onebit or grey16 images. segment_mask = from_pil(mask_img).to_onebit() result_image_rgb = task_image_rgb.mask(segment_mask) result_image = ensure_pixel_type(result_image_rgb, outputs['PNG image (masked)'][0]['resource_type']) result_image.save_PNG(outputs['PNG image (masked)'][0]['resource_path']) # If polygons image output port, write poly file if 'Polygons' in outputs: poly_string = str(polygon_outer_points) f = open(outputs['Polygons'][0]['resource_path'], 'w') f.write(poly_string) f.close()
def convertToGameraImage(self, pilImage): gameraImage = pil_io.from_pil(pilImage) return gameraImage