profile_photo package

Subpackages

Submodules

profile_photo.errors module

exception profile_photo.errors.FileTooLarge(size: int, max_size: int)[source]

Bases: ProfilePhotoError

exception profile_photo.errors.MissingOneOfParams(*params: Params | str, required_if_missing=None)[source]

Bases: ProfilePhotoError

Error raised when one of input parameter(s) is missing.

exception profile_photo.errors.MissingParams(*params: Params | str, required_if_missing=None)[source]

Bases: ProfilePhotoError

Error raised when an input parameter is missing.

exception profile_photo.errors.ProfilePhotoError(message, **log_kwargs)[source]

Bases: Exception

Base exception class for errors raised by this library.

ERR_STATUS = 400

profile_photo.helpers module

class profile_photo.helpers.Util[source]

Bases: object

Helper Utilities.

max_threads = 3
pool: ThreadPoolExecutor = <concurrent.futures.thread.ThreadPoolExecutor object>
static validate_file_len(size: int, _max_size=5000000)[source]

Validate file length is < 5 MB.

The maximum images size as raw bytes passed in as parameter to the Rekognition API is 5 MB.

See: https://docs.aws.amazon.com/rekognition/latest/dg/limits.html#quotas

static validate_params(required_if_missing: Params | None = None, **params)[source]

Validate required parameters are passed in.

profile_photo.log module

profile_photo.main module

Main module.

profile_photo.main.create_headshot(filepath_or_bytes: PathLike[str] | PathLike[bytes] | str | bytes | None = None, *, file_ext: str | None = None, faces: DetectFacesResp | Path | dict | str | None = None, labels: DetectLabelsResp | Path | dict | str | None = None, region: str = 'us-east-1', profile: str | None = None, bucket: str | None = None, key: str | None = None, debug: bool = False, output_dir: PathLike[str] | PathLike[bytes] | str = None) ProfilePhoto[source]

Create a Headshot Photo of a person, given an image.

Parameters:
  • filepath_or_bytes – Path to a local file, or image data as Bytes

  • file_ext – File extension or image type of output data (optional), defaults to the extension of input filename, or .jpg if a filename is not passed in.

  • faces – Cached response for the image, from the AWS Rekognition DetectFaces API

  • labels – Cached response for the image, from the AWS Rekognition DetectLabels API

  • region – AWS region, defaults to us-east-1 if not specified

  • profile – AWS profile name, used for API calls to AWS Rekognition

  • bucket – Bucket name, if the image data lives in an S3 Bucket or is > 5MB in size

  • key – Path to the image (object) in the S3 Bucket

  • debug – True to log debug messages and show the image

  • output_dir – Path to a local folder to save the output image and API responses (optional)

Returns:

a ProfilePhoto object, containing the output image and API response data

profile_photo.models module

class profile_photo.models.Params(value)[source]

Bases: StrEnum

Required Params – one of FILEPATH_OR_BYTES or {FACES, LABELS}.

FACES = 'faces'
FILEPATH_OR_BYTES = 'filepath_or_bytes'
LABELS = 'labels'
class profile_photo.models.ProfilePhoto(filepath: 'str | None', im_bytes: 'bytes', is_rotated: 'bool', orientation: 'int | None', faces: 'DetectFacesResp', labels: 'DetectLabelsResp', _original_im_bytes: 'bytes')[source]

Bases: object

faces: DetectFacesResp
filepath: str | None
im_bytes: bytes
property image: Image

Returns the final photo as a PIL Image.

is_rotated: bool
labels: DetectLabelsResp
orientation: int | None
save_all(folder: Path | str | None = None, get_im_filename: GetImFileName = <function _get_im_filename>, get_response_filename: GetResponseFileName = <function _get_response_filename>)[source]

Save both the output image and API responses to a local folder.

save_image(folder: Path | str | None = None, get_filename: GetImFileName = <function _get_im_filename>)[source]

Save the output image to a local folder.

save_responses(folder: Path | str | None = None, get_filename: GetResponseFileName = <function _get_response_filename>)[source]

Save (or cache) Rekognition API responses – from the Detect Faces API and Detect Labels API – as separate JSON files under a folder or path.

show(side_by_side=True, title='Profile Photo')[source]

Show the Profile Photo (as a PIL Image) in a new window.

If side_by_side is True (the default), then also show the original image on the left.

property side_by_side_image: Image

Returns a horizontally concatenated photo (before and after) as a PIL Image.

class profile_photo.models.StrEnum(value)[source]

Bases: str, Enum

An enumeration.

classmethod values()[source]

Returns an iterable of all values in the Enum subclass.

Module contents

Profile Photo

Center + Crop Image to create a Profile Pic or Headshot

Sample Usage:

>>> from profile_photo import create_headshot
>>> photo = create_headshot('/path/to/image')
>>> photo.show()
>>> # Optional: cache the Rekognition API responses
>>> photo.save_responses('/path/to/folder')

For full documentation and more advanced usage, please see <https://profile-photo.readthedocs.io>.

copyright:
  1. 2023 by Ritvik Nag.

:license:MIT, see LICENSE for more details.

profile_photo.create_headshot(filepath_or_bytes: PathLike[str] | PathLike[bytes] | str | bytes | None = None, *, file_ext: str | None = None, faces: DetectFacesResp | Path | dict | str | None = None, labels: DetectLabelsResp | Path | dict | str | None = None, region: str = 'us-east-1', profile: str | None = None, bucket: str | None = None, key: str | None = None, debug: bool = False, output_dir: PathLike[str] | PathLike[bytes] | str = None) ProfilePhoto[source]

Create a Headshot Photo of a person, given an image.

Parameters:
  • filepath_or_bytes – Path to a local file, or image data as Bytes

  • file_ext – File extension or image type of output data (optional), defaults to the extension of input filename, or .jpg if a filename is not passed in.

  • faces – Cached response for the image, from the AWS Rekognition DetectFaces API

  • labels – Cached response for the image, from the AWS Rekognition DetectLabels API

  • region – AWS region, defaults to us-east-1 if not specified

  • profile – AWS profile name, used for API calls to AWS Rekognition

  • bucket – Bucket name, if the image data lives in an S3 Bucket or is > 5MB in size

  • key – Path to the image (object) in the S3 Bucket

  • debug – True to log debug messages and show the image

  • output_dir – Path to a local folder to save the output image and API responses (optional)

Returns:

a ProfilePhoto object, containing the output image and API response data