Skip to main content

Profile Photo

image image image Documentation Status Updates

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

If this project has helped you, please consider making a donation.

Install

Local tests:

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

$ pip install profile-photo[all]

AWS Lambda deployment:

$ pip install profile-photo

Features

  • Exports a helper function, create_headshot, 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)

Examples

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

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.

Buy me a coffee

Liked some of my work? Buy me a coffee (or more likely a beer)

Buy Me A Coffee

License

Copyright (c) 2023-present Ritvik Nag

Licensed under MIT License

History

0.1.3 (2023-03-13)

  • Update documentation to include sponsor links, and complete install instructions.

0.1.2 (2023-03-09)

  • Embed images in documentation.
  • Convert .rst files to .md format.

0.1.1 (2023-03-08)

  • Update documentation.

0.1.0 (2023-03-08)

  • First release on PyPI.

Metadata

Release files for profile-photo 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for profile-photo 0.1.3
File Size Uploaded Provenance
profile-photo-0.1.3.tar.gz 30.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for profile-photo 0.1.3
File Interpreter ABI Platform
profile_photo-0.1.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 55.1 kB

Release files / profile-photo-0.1.3.tar.gz

Download URL profile-photo-0.1.3.tar.gz
Size 30.1 kB
Tags Source
SHA-256 checksum
How to use checksums
76fd2f070aab8ab127d8dcf03bdeba298dfe7a8dc716c34a85614cd972c603c0
BLAKE2b-256 checksum
How to use checksums
d6e4416b16290104469695c36d0042c4ce51d86985e2c12c37ba40913a63e48d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release files / profile_photo-0.1.3-py2.py3-none-any.whl

Download URL profile_photo-0.1.3-py2.py3-none-any.whl
Size 25.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
4314ea82f6daa4a0ecf8a187ef0e6cb248cd7a198c238e6e1ba90e00c08712cb
BLAKE2b-256 checksum
How to use checksums
61bfa4f214dbafa215bdd7b416d2ffef65a79e1fb36a8b1c50fa752ba8c8e4df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Security for profile-photo 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page