diff --git a/README.md b/README.md index d33472ee..c03f7553 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ import c2pa See the [`examples` directory](https://github.com/contentauth/c2pa-python/tree/main/examples) for some helpful examples: +- `examples/read.py` shows how to read and verify an asset with a C2PA manifest. - `examples/sign.py` shows how to sign and verify an asset with a C2PA manifest. - `examples/training.py` demonstrates how to add a "Do Not Train" assertion to an asset and verify it. diff --git a/examples/README.md b/examples/README.md index ce8003b9..983fc6a8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -75,7 +75,13 @@ except Exception as err: To run the examples, make sure you have the c2pa-python package installed (`pip install c2pa-python`) and you're in the root directory of the project. We recommend working using virtual environments (venv). Then run the examples as shown below. -Run the "do not train" assertion example: +### Run the reading C2PA data example + +```bash +python examples/read.py +``` + +### Run the "do not train" assertion example: ```bash python examples/training.py diff --git a/examples/read.py b/examples/read.py new file mode 100644 index 00000000..ea30126b --- /dev/null +++ b/examples/read.py @@ -0,0 +1,47 @@ +import sys +import c2pa +import urllib.request + + +# This example shows how to read a C2PA manifest embedded in a media file, and validate +# that it is trusted according to the official trust anchor certificate list. +# The output is printed as prettified JSON. + +TRUST_ANCHORS_URL = "https://contentcredentials.org/trust/anchors.pem" + + +def load_trust_anchors(): + try: + with urllib.request.urlopen(TRUST_ANCHORS_URL) as response: + anchors = response.read().decode('utf-8') + settings = { + "verify": { + "verify_cert_anchors": True + }, + "trust": { + "trust_anchors": anchors + } + } + c2pa.load_settings(settings) + except Exception as e: + print(f"Warning: Could not load trust anchors from {TRUST_ANCHORS_URL}: {e}") + + +def read_c2pa_data(media_path: str): + print(f"Reading {media_path}") + try: + with c2pa.Reader(media_path) as reader: + print(reader.detailed_json()) + except Exception as e: + print(f"Error reading C2PA data from {media_path}: {e}") + sys.exit(1) + + +if __name__ == '__main__': + if len(sys.argv) < 2: + media_path = "tests/fixtures/cloud.jpg" + else: + media_path = sys.argv[1] + + load_trust_anchors() + read_c2pa_data(media_path)