Skip to main content

Center + Crop Image to create a Profile Pic or Headshot

Project description

Profile Photo

https://img.shields.io/pypi/v/profile-photo.svg https://img.shields.io/pypi/pyversions/profile-photo.svg https://github.com/rnag/profile-photo/actions/workflows/dev.yml/badge.svg Documentation Status Updates

Center + Crop Image to create a Profile Pic or Headshot.

Check out example images on GitHub for sample use cases and results.

Install

$ pip install profile-photo[all]

The [all] extra installs boto3, which is excluded by default - this assumes an AWS environment.

Features

  • Exports a helper function, create_headshot used to create a close-up or headshot of the primary face in a photo or image

  • Leverages Amazon Rekognition to detect bounding boxes of a person’s Face and relevant Labels, such as Person

  • Exposes helper methods to save the result image (cropped) as well as API responses to a local folder

Usage

Basic usage, with a sample image:

from urllib.request import urlopen

from profile_photo import create_headshot


# Set the $AWS_PROFILE environment variable instead
aws_profile = 'my-profile'

im_url = 'https://raw.githubusercontent.com/rnag/profile-photo/main/examples/woman-2.jpeg'
im_bytes = urlopen(im_url).read()

photo = create_headshot(im_bytes, profile=aws_profile)
photo.show()

An example with a local image, and saving the result image and API responses to a folder:

from __future__ import annotations

from profile_photo import create_headshot


# customize local file location for API responses
def get_filename(file_name: str | None, api: str):
    return f'responses/{file_name}_{api}.json'


photo = create_headshot('/path/to/image')

# this saves image and API responses to a results/ folder
# can also be achieved by passing `output_dir` above
photo.save_all('results', get_response_filename=get_filename)

# display before-and-after images
photo.show()

Lastly, an example with an image on S3, and passing in cached Rekognition API responses for the image:

from pathlib import Path

from profile_photo import create_headshot


s3_image_path = Path('path/to/image.jpg')
responses_dir = Path('./my/responses')

_photo = create_headshot(bucket='my-bucket',
                         key=str(s3_image_path),
                         profile='my-profile',
                         faces=responses_dir / f'{s3_image_path.stem}_DetectFaces.json',
                         labels=responses_dir / f'{s3_image_path.stem}_DetectLabels.json',
                         debug=True)

How It Works

This library currently makes calls to the Amazon Rekognition APIs to detect bounding boxes on a Face and Person in a photo.

It then uses custom, in-house logic to determine the X/Y coordinates for cropping. This mainly involves “blowing up” or enlarging the Face bounding box, but then correcting the coordinates as needed by the Person box. This logic has been fine-tuned based on what I have found provide the best overall results for generic images (not necessary profile photos).

In the future, other ideas other than Rekognition might be considered – such as existing machine learning approaches or even a solution with the opencv library in Python alone.

Future Ideas

  • Support background removal with rembg.

  • Investigate other (alternate) approaches to Rekognition for detecting a face and person in a photo.

Credits

This package was created with Cookiecutter and the rnag/cookiecutter-pypackage project template.

History

0.1.1 (2023-03-08)

  • Update documentation.

0.1.0 (2023-03-08)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

profile-photo-0.1.1.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

profile_photo-0.1.1-py2.py3-none-any.whl (24.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file profile-photo-0.1.1.tar.gz.

File metadata

  • Download URL: profile-photo-0.1.1.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for profile-photo-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aa194c02dc426fcfcf2bc91b152cd09b2b63b7c8e9f3035bb11f8f4b9f174cef
MD5 23a22a8b4d9de02e195e1a3ba68add9c
BLAKE2b-256 08866cfc41df441a55368a7acec7320a19a109773988781b34a5ac8f254ea005

See more details on using hashes here.

File details

Details for the file profile_photo-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for profile_photo-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f15884d24f19170bfd66d19d801fdfb592adea741d58f98d8437d48f38e84093
MD5 1f63c5d0e85d2cc1d9f833136af194fa
BLAKE2b-256 658267bfb2b6d78a27db0652206e8faf35e0a0f2dd056e89187f8f9f71a1887b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page