def test_resize_with_fit(self): filename = os.path.join(BASE_DIR, 'terminal.gif') # 35x28 base_ops = {'w': 50, 'h': 100} operations = copy(base_ops) operations.update({'fit': 'crop'}) output = image_transform(filename, operations) img = PIL.Image.open(output) self.assertEqual((operations['w'], operations['h']), img.size) # # `clip` == `bounds` which will fit only one the # dimensions in case the aspect ratio is different # operations = copy(base_ops) operations.update({'fit': 'bounds'}) output = image_transform(filename, operations) img = PIL.Image.open(output) self.assertEqual((operations['w'], 40), img.size) operations = copy(base_ops) operations.update({'fit': 'clip'}) output = image_transform(filename, operations) img = PIL.Image.open(output) self.assertEqual((operations['w'], 40), img.size)
def test_quality(self): filename = os.path.join(BASE_DIR, 'lincoln.jpg') w, h = PIL.Image.open(filename).size # # applying no transformation with the default quality # operations = {'w': w, 'h': h} output = image_transform(filename, operations) out_file_size = os.stat(output).st_size operations = {'q': DEFAULT_QUALITY_RATE - 5} output = image_transform(filename, operations) self.assertLess(os.stat(output).st_size, out_file_size)
def test_dpr(self): filename = os.path.join(BASE_DIR, 'lincoln.jpg') w, h = PIL.Image.open(filename).size dpr = 2 operations = {'dpr': dpr} output = image_transform(filename, operations) ow, oh = PIL.Image.open(output).size self.assertEqual((w*dpr, h*dpr), (ow, oh)) scale = 2 w, h = w*scale, h*scale operations = {'dpr': dpr, 'w': w, 'h': h} output = image_transform(filename, operations) ow, oh = PIL.Image.open(output).size self.assertEqual((w*dpr, h*dpr), (ow, oh))
def test_resize(self): filename = os.path.join(BASE_DIR, 'terminal.gif') # 35x28 operations = {'w': 50, 'h': 100} output = image_transform(filename, operations) img = PIL.Image.open(output) self.assertEqual((operations['w'], operations['h']), img.size) operations = {'w': 25} output = image_transform(filename, operations) img = PIL.Image.open(output) self.assertEqual(operations['w'], img.size[0]) operations = {'h': 25} output = image_transform(filename, operations) img = PIL.Image.open(output) self.assertEqual(operations['h'], img.size[1])
def main(image_path): """ Main runs the program to average the faces in a given file path, displaying the 'average' face at the end. **Parameters** image_path: str A string indicating the filepath containing the images to be 'averaged' **Returns** None """ print('Opening {} and checking for faces...'.format(image_path)) print('Processing images...') face_check(image_path) images = process_images(image_path) print('Finding facial landmarks...') allandmarks = find_landmarks(image_path) print('Scaling images to common space...') scaled_images = scale_images(images, allandmarks) pointsAvg, pointsNorm = scale_landmarks(images, allandmarks) print('Triangulating points...') dt = calculateDelaunayTriangles(np.array(pointsAvg)) print('Averaging faces...') output = image_transform(scaled_images, pointsNorm, pointsAvg, dt) print('Success!') cv2.imshow('image', output) cv2.waitKey(0)
def test_auto_compress(self): filename = os.path.join(BASE_DIR, 'lincoln.jpg') w, h = PIL.Image.open(filename).size in_file_size = os.stat(filename).st_size w, h = PIL.Image.open(filename).size operations = {'q': AGRESSIVE_QUALITY_RATE} output = image_transform(filename, operations) size_with_metadata = os.stat(output).st_size self.assertLess(os.stat(output).st_size, in_file_size) # applies agressive quality and remove metadata operations = {'auto': 'compress'} output = image_transform(filename, operations) self.assertLess(os.stat(output).st_size, in_file_size) size_with_no_metadata = os.stat(output).st_size self.assertLess(size_with_no_metadata, size_with_metadata)
def test_all_ops(self): for img in ALL_IMAGES: filename = os.path.join(BASE_DIR, img) side = 100 dpr = 2 operations = {'w': side,'h': side, 'fit': 'crop', 'fm': 'gif', 'dpr': dpr, 'q': 50, 'auto': 'compress'} output = image_transform(filename, operations) ow, oh = PIL.Image.open(output).size self.assertEqual((side*dpr, side*dpr), (ow, oh))
def test_format(self): filename = os.path.join(BASE_DIR, 'lincoln.jpg') in_file_size = os.stat(filename).st_size operations = {'fm': 'png'} output = image_transform(filename, operations) self.assertIn('png', output) self.assertGreater(os.stat(output).st_size, in_file_size) filename = os.path.join(BASE_DIR, 'terminal.gif') in_file_size = os.stat(filename).st_size operations = {'fm': 'jpeg', 'q': 10} output = image_transform(filename, operations) self.assertIn('jpeg', output) self.assertGreater(os.stat(output).st_size, in_file_size) for fm in SUPPORTED_FORMATS: #FIXME: https://github.com/caffeinetv/snappy/issues/10 if fm != 'webp': operations = {'fm': fm} output = image_transform(filename, operations) self.assertIn(fm, output)
def main(image_path): """ Main runs the program to average the faces in a given file path, saving the averaged image in an output image file and clearing the original image path. **Parameters** image_path: str A string indicating the filepath containing the images to be 'averaged' **Returns** None """ print('Opening {} and checking for faces...'.format(image_path)) print('Processing images...') face_check(image_path) images = process_images(image_path) print('Finding facial landmarks...') allandmarks = find_landmarks(image_path) print('Scaling images to common space...') scaled_images = scale_images(images, allandmarks) pointsAvg, pointsNorm = scale_landmarks(images, allandmarks) print('Triangulating points...') dt = calculateDelaunayTriangles(np.array(pointsAvg)) print('Averaging faces...') output = image_transform(scaled_images, pointsNorm, pointsAvg, dt) print('Success!') output = output * 255 output = output.astype('uint8') cv2.imwrite('static/outputimage/average_face.png', output) os.chdir('./static/faces') for file in os.listdir('.'): print(file) if file.endswith('.jpg'): os.remove(file) elif file.endswith('.tiff'): os.remove(file) elif file.endswith('.jpeg'): os.remove(file) elif file.endswith('.png'): os.remove(file) else: continue os.chdir('../..')