Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 7 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions examples/read.py
Original file line number Diff line number Diff line change
@@ -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)
Loading