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.
NOTE: This is a completely different API from 0.4.0. Check Release notes for changes.
WARNING: This is an 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 that works on Windows, macOS, and most Linux distributions (using manylinux). If you need to run on another platform, see Development for information on how to build from source.
Reinstalling
If you tried unsuccessfully to install this package before the 0.40 release, then use this command to reinstall:
pip install --upgrade --force-reinstall c2pa-python
Usage
Import
Import the API as follows:
from c2pa import *
Read and validate C2PA data in a file
Use the Reader
to read C2PA data from the specified file.
This examines the specified media file for C2PA data and generates a 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.
The manifests may contain binary resources such as thumbnails which can be retrieved with resource_to_stream
or resource_to_file
using the associated identifier
field values and a uri
.
NOTE: For a comprehensive reference to the JSON manifest structure, see the Manifest store reference.
try:
reader = c2pa.Reader("path/to/media_file.jpg")
# Print the JSON for a manifest.
print("manifest store:", reader.json())
# Get the active manifest.
manifest = reader.get_active_manifest()
if manifest != None:
# get the uri to the manifest's thumbnail and write it to a file
uri = manifest["thumbnail"]["identifier"]
reader.resource_to_file(uri, "thumbnail_v2.jpg")
except Exception as err:
print(err)
Add a signed manifest to a media file
Use a Builder
to add a manifest to an asset.
try:
# Define a function to sign the claim bytes
# In this case we are using a pre-defined sign_ps256 method, passing in our private cert
# Normally this cert would be kept safe in some other location
def private_sign(data: bytes) -> bytes:
return sign_ps256(data, "tests/fixtures/ps256.pem")
# read our public certs into memory
certs = open(data_dir + "ps256.pub", "rb").read()
# Create a signer from the private signer, certs and a time stamp service url
signer = create_signer(private_sign, SigningAlg.PS256, certs, "http://timestamp.digicert.com")
# Define a manifest with thumbnail and an assertion.
manifest_json = {
"claim_generator_info": [{
"name": "python_test",
"version": "0.1"
}],
"title": "Do Not Train Example",
"thumbnail": {
"format": "image/jpeg",
"identifier": "thumbnail"
},
"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" }
}
}
}
]
}
# Create a builder add a thumbnail resource and an ingredient file.
builder = Builder(manifest_json)
# The uri provided here "thumbnail" must match an identifier in the manifest definition.
builder.add_resource_file("thumbnail", "tests/fixtures/A_thumbnail.jpg")
# Define an ingredient, in this case a parent ingredient named A.jpg, with a thumbnail
ingredient_json = {
"title": "A.jpg",
"relationship": "parentOf", # "parentOf", "componentOf" or "inputTo"
"thumbnail": {
"identifier": "thumbnail",
"format": "image/jpeg"
}
}
# Add the ingredient to the builder loading information from a source file.
builder.add_ingredient_file(ingredient_json, "tests/fixtures/A.jpg")
# At this point we could archive or unarchive our Builder to continue later.
# In this example we use a bytearray for the archive stream.
# all ingredients and resources will be saved in the archive
archive = io.BytesIO(bytearray())
builder.to_archive(archive)
archive.seek()
builder = builder.from_archive(archive)
# Sign and add our manifest to a source file, writing it to an output file.
# This returns the binary manifest data that could be uploaded to cloud storage.
c2pa_data = builder.sign_file(signer, "tests/fixtures/A.jpg", "target/out.jpg")
except Exception as err:
print(err)
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" }
}
}
}
]
})
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 |
Development
It is best to set up a virtual environment for development and testing. To build from source on Linux, install curl
and rustup
then set up Python.
First update apt
then (if needed) install curl
:
apt update
apt install curl
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
Build using manylinux by using a Docker image as follows:
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
For example:
source .venv/bin/activate
maturin develop
python3 tests/training.py
deactivate
Release notes
Version 0.5.0
- This release rewrites the API to be stream based using a Builder and Reader model.
- The functions now support throwing c2pa.Error values, caught with try/except.
- Instead of
c2pa.read_file
you now callc2pa_api.Reader.from_file
andreader.json
. - Read thumbnails and other resources use
reader.resource_to_stream
orreader.resource.to_file
. - Instead of
c2pa.sign_file
usec2pa_api.Builder.from_json
andbuilder.sign
orbuilder.sign_file
. - Add thumbnails or other resources with
builder.add_resource
orbuilder.add_resource_file
. - Add Ingredients with
builder.add_ingredient
orbuilder.add_ingredient_file
. - You can archive a
Builder
usingbuilder.to_archive
and reconstruct it withbuilder.from_archive
. - Signers can be constructed with
c2pa_api.create_signer
. - The signer now requires a signing function to keep private keys private.
- Example signing functions are provided in c2pa_api.py
Version 0.4.0
This release:
- Changes the name of the import from
c2pa-python
toc2pa
. - Supports pre-built platform wheels for macOS, Windows and manylinux.
Version 0.3.0
This release includes some breaking changes to align with future APIs:
C2paSignerInfo
moves thealg
to the first parameter from the 3rd.c2pa.verify_from_file_json
is nowc2pa.read_file
.c2pa.ingredient_from_file_json
is nowc2pa.read_ingredient_file
.c2pa.add_manifest_to_file_json
is 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
Hashes for c2pa_python-0.5.0-py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d2fcb4ef9370d742c7c36ed3075c14bfc2f84841591e7c7152db0fdd4b3cc8d |
|
MD5 | 64be8bb90108d9e49fa604f1c51fb687 |
|
BLAKE2b-256 | e0d1b74d3f8177349c7a43cffad9ac2b19bde787aef2dd5c94ae36aa49e3cc88 |
Hashes for c2pa_python-0.5.0-py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28770b855e88cb4646be61140f56ba7a76ff650f91b0f7f71274f412561204b6 |
|
MD5 | 1a15665418a3808521f000f634a68941 |
|
BLAKE2b-256 | 306e7b8ab7f47b0d3cf07f04e9c675d05376aad8ef1db3d85bf8d48271b31863 |
Hashes for c2pa_python-0.5.0-py3-none-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 652382d6fea721c727fb21e010179f608629c96a8ff98bdf78b6864d0da907c7 |
|
MD5 | faee3ce96fd6891f545441c889f30759 |
|
BLAKE2b-256 | cb679db3f1306f29a9be08da0b7dc067068f1b8cc0088bab91872fa6bb58bb1b |
Hashes for c2pa_python-0.5.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cf2cc5d5268b0f0bde83a553bdae3b55b7d253053269a6885f83d77999c2520 |
|
MD5 | 47f752ccbf83755c0f56404a95d8c2b6 |
|
BLAKE2b-256 | d9a46af52fbe85ecc0729b3aa2ff0c04e732bf996eb49a69379bbe0bc2606071 |
Hashes for c2pa_python-0.5.0-py3-none-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7e32c75bb2eac2501fada445f3d333a0c355ced042ab21948e9d939daf968cd |
|
MD5 | ea67ed96a3534693d53ef1ea906ef37e |
|
BLAKE2b-256 | d2c3a71609c1e4178397f48bccc4c09ceece09f40ef7d67c25106a89b5a590ec |
Hashes for c2pa_python-0.5.0-py3-none-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4f4511723bed12ad6f2a10e5ce509714b7b6c311a6cc635f463bcd0fce03524 |
|
MD5 | 64e7fa0a861ab4d6ac77295906966470 |
|
BLAKE2b-256 | c0c351c52b232628ef62c783e581c7710845e43a4b0235abad5d830e9aafb8bf |