Python bindings for the C2PA Content Authenticity Initiative (CAI) library
Project description
C2PA Python
Python bindings for the C2PA Content Authenticity Initiative (CAI) library.
This library enables you to read and validate C2PA data in supported media files and add signed manifests to supported media files.
WARNING: This is an early prerelease version of this library. There may be bugs and unimplemented features, and the API is subject to change.
Installation
Install from PyPI by entering this command:
pip install -U c2pa-python
This is a platform wheel built with Rust. If your platform is not already supported, see the development section for info on how to build from source.
## Usage
### Import
Import the C2PA module as follows:
```py
import c2pa
Read and validate C2PA data in a file
Use the read_file function to read C2PA data from the specified file:
json_store = c2pa.read_file("path/to/media_file.jpg", "path/to/data_dir")
This function examines the specified media file for C2PA data and generates a JSON report of any data it finds. If there are validation errors, the report includes a validation_status field. For a summary of supported media types, see Supported file formats.
A media file may contain many manifests in a manifest store. The most recent manifest is identified by the value of the active_manifest field in the manifests map.
If the optional data_dir is provided, the function extracts any binary resources, such as thumbnails, icons, and C2PA data into that directory. These files are referenced by the identifier fields in the manifest store report.
NOTE: For a comprehensive reference to the JSON manifest structure, see the CAI manifest store reference.
Add a signed manifest to a media file
Use the sign_file function to add a signed manifest to a media file.
result = c2pa.sign_file("path/to/source.jpg",
"path/to/dest.jpg",
manifest_json,
sign_info,
data_dir)
The parameters (in order) are:
- The source (original) media file.
- The destination file that will contain a copy of the source file with the manifest data added.
manifest_json, a JSON-formatted string containing the manifest data you want to add; see Creating a manifest JSON definition file below.sign_info, aSignerInfoobject instance; see Generating SignerInfo below.data_diroptionally specifies a directory path from which to load resource files referenced in the manifest JSON identifier fields; for example, thumbnails, icons, and manifest data for ingredients.
Create a SignerInfo Instance
A SignerInfo object contains information about a signature. To create an instance of SignerInfo, first set up the signer information from the public and private key .pem files as follows:
certs = open("path/to/public_certs.pem","rb").read()
prv_key = open("path/to/private_key.pem","rb").read()
Then create a new SignerInfo instance using the keys as follows, specifying the signing algorithm used and optionally a time stamp authority URL:
sign_info = c2pa.SignerInfo("es256", certs, priv_key, "http://timestamp.digicert.com")
For the list of supported signing algorithms, see Creating and using an X.509 certificate.
Creating a manifest JSON definition file
The manifest JSON string defines the C2PA manifest to add to the file.
manifest_json = json.dumps({
"claim_generator": "python_test/0.1",
"assertions": [
{
"label": "c2pa.training-mining",
"data": {
"entries": {
"c2pa.ai_generative_training": { "use": "notAllowed" },
"c2pa.ai_inference": { "use": "notAllowed" },
"c2pa.ai_training": { "use": "notAllowed" },
"c2pa.data_mining": { "use": "notAllowed" }
}
}
}
]
})
Development
It is best to set up a virtual environment for development and testing.
To build from source on Linux, install curl and rustup and set up python
First update apt
apt update
Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
Install Python, pip and venv
apt install python3
apt install pip
apt install python3.11-venv
python3 -m venv .venv
Build the wheel for your platform
source .venv/bin/activate
pip install maturin
pip install uniffi-bindgen
python3 -m pip install build
pip install -U pytest
python3 -m build --wheel
ManyLinux build
docker run -it quay.io/pypa/manylinux_2_28_aarch64 bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
export PATH=/opt/python/cp312-cp312/bin:$PATH
pip install maturin
pip install venv
pip install build
pip install -U pytest
cd home
git clone https://github.com/contentauth/c2pa-python.git
cd c2pa-python
python3 -m build --wheel
auditwheel repair target/wheels/c2pa_python-0.4.0-py3-none-linux_aarch64.whl
Testing
We use PyTest for testing.
Run tests by entering this command:
source .venv/bin/activate
maturin develop
pytest
deactivate
Example
Run the example code like this:
source .venv/bin/activate
maturin develop
python3 tests/training.py
deactivate
Supported file formats
| Extensions | MIME type |
|---|---|
avi |
video/msvideo, video/avi, application-msvideo |
avif |
image/avif |
c2pa |
application/x-c2pa-manifest-store |
dng |
image/x-adobe-dng |
heic |
image/heic |
heif |
image/heif |
jpg, jpeg |
image/jpeg |
m4a |
audio/mp4 |
mp4 |
video/mp4, application/mp4 |
mov |
video/quicktime |
png |
image/png |
svg |
image/svg+xml |
tif,tiff |
image/tiff |
wav |
audio/x-wav |
webp |
image/webp |
Change Notes:
Version 0.3.0 changes: There are some breaking changes to align with future APIs:
C2paSignerInfomoves thealgto the first parameter from the 3rd.c2pa.verify_from_file_jsonis nowc2pa.read_file.c2pa.ingredient_from_file_jsonis nowc2pa.read_ingredient_file.c2pa.add_manifest_to_file_jsonis nowc2pa.sign_file.- There are many more specific errors types now, and Error messages always start with the name of the error i.e (str(err.value).startswith("ManifestNotFound")).
- The ingredient thumbnail identifier may be jumbf uri reference if a valid thumb already exists in the active manifest.
- Extracted file paths for read_file now use a folder structure and different naming conventions.
License
This package is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
Note that some components and dependent crates are licensed under different terms; please check the license terms for each crate and component for details.
Contributions and feedback
We welcome contributions to this project. For information on contributing, providing feedback, and about ongoing work, see Contributing.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file c2pa_python-0.4.0.tar.gz.
File metadata
- Download URL: c2pa_python-0.4.0.tar.gz
- Upload date:
- Size: 108.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06101b536265c7b8eee1dd87b729eca1017faf176220c84b7a598e7ebee46450
|
|
| MD5 |
7ee9c3891d0f4a02c2ff260592d53038
|
|
| BLAKE2b-256 |
9c4d3c21ab17831494b2555c4347227754a2fedf3061393b7d9c08dde69a9fd4
|
File details
Details for the file c2pa_python-0.4.0-py3-none-win_amd64.whl.
File metadata
- Download URL: c2pa_python-0.4.0-py3-none-win_amd64.whl
- Upload date:
- Size: 4.0 MB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1fd9b817fce8473fe4bd6b9884a6a06d6072d2bccace86ce1bd72ef696d15ad5
|
|
| MD5 |
fe70411125b9605f0672c47c35d955c9
|
|
| BLAKE2b-256 |
a8d9215ecd0eb071f53f5aee1132a6e264982a877f664b14551ec1cd777f7413
|
File details
Details for the file c2pa_python-0.4.0-py3-none-win32.whl.
File metadata
- Download URL: c2pa_python-0.4.0-py3-none-win32.whl
- Upload date:
- Size: 3.6 MB
- Tags: Python 3, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eeee1023a7414cf691d83e9d3da08a5d806142989a17a070cf8cfc69a4c89ce5
|
|
| MD5 |
ce7f897b8459b02a53c26003ad47687b
|
|
| BLAKE2b-256 |
87dc70db3b930ba73511ab21371db6115bd92cefa5220bc8ecd7a9795a296038
|
File details
Details for the file c2pa_python-0.4.0-py3-none-manylinux_2_28_aarch64.whl.
File metadata
- Download URL: c2pa_python-0.4.0-py3-none-manylinux_2_28_aarch64.whl
- Upload date:
- Size: 5.2 MB
- Tags: Python 3, manylinux: glibc 2.28+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0053a8f8873f0a80a4ea1d463feb22db9b05cf031d25765502d3326d778df2f
|
|
| MD5 |
c281c2ee8e274da63beb50d3bb442b12
|
|
| BLAKE2b-256 |
0a0641d0beecce9cddd1b33c7e5b14b1d4d713ed4040af76218ccc85054ac4da
|
File details
Details for the file c2pa_python-0.4.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: c2pa_python-0.4.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 6.0 MB
- Tags: Python 3, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d441acc7ebd217da6f1c31b1e06d26a12b2e31cc051b8e0d26e2c89866b25c7
|
|
| MD5 |
2ed0a43eace0b07816f14efa2293f58d
|
|
| BLAKE2b-256 |
62f751ded1e14a38a541764805595fd6455405899bd6da1e4d61cda540fc4724
|
File details
Details for the file c2pa_python-0.4.0-py3-none-macosx_11_0_arm64.whl.
File metadata
- Download URL: c2pa_python-0.4.0-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 3.8 MB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce30c6dfc55d8563e5b01cb727b90e3f1b7200dba578c8440a9de20f13b67b15
|
|
| MD5 |
0c53ce5d0055abf2e87bb1598d64ec79
|
|
| BLAKE2b-256 |
702036d9f85525f254d6c7102fe2cace531d08e4f3d8fc1a49a8f66432039e3c
|
File details
Details for the file c2pa_python-0.4.0-py3-none-macosx_10_12_x86_64.whl.
File metadata
- Download URL: c2pa_python-0.4.0-py3-none-macosx_10_12_x86_64.whl
- Upload date:
- Size: 4.1 MB
- Tags: Python 3, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.4.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2f53a0f74ff59ee160151ef24a7c4593b9d7ca2fa796caf96eb3a65edf260e4
|
|
| MD5 |
7ade973c342e4f107761e651a8ef7e21
|
|
| BLAKE2b-256 |
9b342f10929ffcc10541e5501693943901f1bff391cfcfe3c16a3a115f968f9f
|