Skip to content

Commit 05843d9

Browse files
authored
docs: Add example read.py script (#207)
* Expose `load_settings()` and `version()` for import to allow specifying custom settings (like a certificate trust list). * Add an example read.py script to show how to read a file's C2PA manifest and verify it. * Address PR comments * Remove __init__.py changes
1 parent 88a756d commit 05843d9

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import c2pa
3434

3535
See the [`examples` directory](https://github.com/contentauth/c2pa-python/tree/main/examples) for some helpful examples:
3636

37+
- `examples/read.py` shows how to read and verify an asset with a C2PA manifest.
3738
- `examples/sign.py` shows how to sign and verify an asset with a C2PA manifest.
3839
- `examples/training.py` demonstrates how to add a "Do Not Train" assertion to an asset and verify it.
3940

examples/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ except Exception as err:
7575

7676
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.
7777

78-
Run the "do not train" assertion example:
78+
### Run the reading C2PA data example
79+
80+
```bash
81+
python examples/read.py
82+
```
83+
84+
### Run the "do not train" assertion example:
7985

8086
```bash
8187
python examples/training.py

examples/read.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
import c2pa
3+
import urllib.request
4+
5+
6+
# This example shows how to read a C2PA manifest embedded in a media file, and validate
7+
# that it is trusted according to the official trust anchor certificate list.
8+
# The output is printed as prettified JSON.
9+
10+
TRUST_ANCHORS_URL = "https://contentcredentials.org/trust/anchors.pem"
11+
12+
13+
def load_trust_anchors():
14+
try:
15+
with urllib.request.urlopen(TRUST_ANCHORS_URL) as response:
16+
anchors = response.read().decode('utf-8')
17+
settings = {
18+
"verify": {
19+
"verify_cert_anchors": True
20+
},
21+
"trust": {
22+
"trust_anchors": anchors
23+
}
24+
}
25+
c2pa.load_settings(settings)
26+
except Exception as e:
27+
print(f"Warning: Could not load trust anchors from {TRUST_ANCHORS_URL}: {e}")
28+
29+
30+
def read_c2pa_data(media_path: str):
31+
print(f"Reading {media_path}")
32+
try:
33+
with c2pa.Reader(media_path) as reader:
34+
print(reader.detailed_json())
35+
except Exception as e:
36+
print(f"Error reading C2PA data from {media_path}: {e}")
37+
sys.exit(1)
38+
39+
40+
if __name__ == '__main__':
41+
if len(sys.argv) < 2:
42+
media_path = "tests/fixtures/cloud.jpg"
43+
else:
44+
media_path = sys.argv[1]
45+
46+
load_trust_anchors()
47+
read_c2pa_data(media_path)

0 commit comments

Comments
 (0)