def test_scaling(self): im = get_img_arr('imgs/castle_small.jpg') h, w = new_shape_for_ratio(im, 315, 851, scale_x=False) assert (abs(h / w - 315 / 851) < 0.001) h, w = new_shape_for_ratio(im, 215, 851) assert (abs(h / w - 215 / 851) < 0.001)
def test_import_rect(self): rectangle_img = get_img_arr("imgs/HJoceanSmall.png") self.assertEqual(rectangle_img.shape, (285, 507, 3))
def test_import_square(self): square_img = get_img_arr("imgs/mountain_icon.jpg") self.assertEqual(square_img.shape, (64, 64, 3))
def main(): parser = argparse.ArgumentParser( description="Intelligently crop an image along one axis") parser.add_argument('input_file') parser.add_argument('-a', '--axis', required=True, help="What axis to shrink the image on.", choices=['x', 'y']) parser.add_argument('-p', '--pixels', type=int, required=True, help="How many pixels to shrink the image by.") parser.add_argument('-o', '--output', help="What to name the new cropped image.") parser.add_argument('-i', '--interval', type=int, help="Save every i intermediate images.") parser.add_argument( '-b', '--border', type=bool, help= "Whether or not to pad the cropped images to the size of the original") parser.add_argument( '-s', '--show_seam', type=bool, help="Whether to highlight the removed seam on the intermediate images." ) args = vars(parser.parse_args()) print(args) img = get_img_arr(args['input_file']) if args['axis'] == 'y': img = np.transpose(img, axes=(1, 0, 2)) if args['output'] is None: name = args['input_file'].split('.') args['output'] = name[0] + '_crop.' + name[1] savepoints = every_n(args['interval'], img.shape[1]) if args['interval'] else None cropped_img = resize_image(img, args['pixels'], dual_gradient_energy, save_name=args['output'], savepoints=savepoints, rotated=args['axis'] == 'y', pad=args['border'], highlight=args['show_seam']) if args['axis'] == 'y': cropped_img = np.transpose(cropped_img, axes=(1, 0, 2)) if args['border']: h, w = img.shape[:2] if args['axis'] == 'y': h, w = w, h cropped_img = pad_img(cropped_img, h, w) cropped_img.save(args['output']) else: Image.fromarray(cropped_img).save(args['output']) print( "\nImage {0} cropped by {1} pixels along the {2}-axis and saved as {3}\n" .format(args['input_file'], args['pixels'], args['axis'], args['output']))