Skip to content

Commit b32a7cf

Browse files
authored
Merge branch 'main' into mathern/docs-update
2 parents 1ab3c10 + ae4ed38 commit b32a7cf

File tree

1 file changed

+79
-14
lines changed

1 file changed

+79
-14
lines changed

examples/README.md

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,99 @@
1-
# Usage examples
1+
# Python example code
22

3-
## Examples
3+
The `examples` directory contains some small examples of using the Python library.
4+
The examples use asset files from the `tests/fixtures` directory, save the resulting signed assets to the temporary `output` directory, and display manifest store data and other output to the console.
45

5-
### Adding a "Do Not Train" Assertion
6+
## Signing and verifying assets
67

7-
The `examples/training.py` script demonstrates how to add a "Do Not Train" assertion to an asset and verify it.
8+
The [`examples/sign.py`](https://github.com/contentauth/c2pa-python/blob/main/examples/sign.py) script shows how to sign an asset with a C2PA manifest and verify the asset.
89

9-
### Signing and Verifying Assets
1010

1111
The `examples/sign.py` script shows how to sign an asset with a C2PA manifest and verify it using a callback signer. Callback signers let you define signing logic, eg. where to load keys from.
1212

1313
The `examples/sign_info.py` script shows how to sign an asset with a C2PA manifest and verify it using a "default" signer created with the needed signer information.
14+
These statements create a `builder` object with the specified manifest JSON (omitted in the snippet below), call `builder.sign()` to sign and attach the manifest to the source file, `tests/fixtures/C.jpg`, and save the signed asset to the output file, `output/C_signed.jpg`:
1415

15-
## Running the Examples
16+
```py
17+
manifest_definition = {
18+
// ... JSON omitted here
19+
}
1620

17-
To run the examples, make sure you have the c2pa-python package installed and you're in the root directory of the project. We recommend working using virtual environments (venv).
21+
builder = c2pa.Builder(manifest_definition)
1822

19-
Then you can run the examples with the following commands:
23+
with open(fixtures_dir + "C.jpg", "rb") as source:
24+
with open(output_dir + "C_signed.jpg", "wb") as dest:
25+
result = builder.sign(signer, "image/jpeg", source, dest)
26+
```
27+
28+
Then these statements read and verify the signed asset:
29+
30+
```py
31+
print("\nReading signed image metadata:")
32+
with open(output_dir + "C_signed.jpg", "rb") as file:
33+
reader = c2pa.Reader("image/jpeg", file)
34+
print(reader.json())
35+
```
36+
37+
## Adding a "do not train" assertion
38+
39+
The [`examples/training.py`](https://github.com/contentauth/c2pa-python/blob/main/examples/training.py) script shows how to add a "do not train" assertion to an asset, then verify the asset and display to the console whether its manifest indicates ML training is allowed.
40+
41+
These statements sign the asset using a stream:
42+
43+
```py
44+
with open(testFile, "rb") as source_file:
45+
with open(testOutputFile, "wb") as dest_file:
46+
result = builder.sign(signer, "image/jpeg", source_file, dest_file)
47+
```
48+
49+
These statements verify the asset and check its attached manifest for a "do not train" assertion:
50+
51+
```py
52+
allowed = True # opt out model, assume training is ok if the assertion doesn't exist
53+
try:
54+
# Create reader using the current API
55+
reader = c2pa.Reader(testOutputFile)
56+
manifest_store = json.loads(reader.json())
57+
58+
manifest = manifest_store["manifests"][manifest_store["active_manifest"]]
59+
for assertion in manifest["assertions"]:
60+
if assertion["label"] == "c2pa.training-mining":
61+
if getitem(assertion, ("data","entries","c2pa.ai_training","use")) == "notAllowed":
62+
allowed = False
63+
64+
# get the ingredient thumbnail and save it to a file using resource_to_stream
65+
uri = getitem(manifest,("ingredients", 0, "thumbnail", "identifier"))
66+
with open(output_dir + "thumbnail_v2.jpg", "wb") as thumbnail_output:
67+
reader.resource_to_stream(uri, thumbnail_output)
68+
69+
except Exception as err:
70+
sys.exit(err)
71+
```
72+
73+
## Running the examples
74+
75+
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.
76+
77+
Run the "do not train" assertion example:
2078

2179
```bash
22-
# Run the "Do Not Train" assertion example
2380
python examples/training.py
81+
```
82+
83+
### Run the signing and verification example:
84+
85+
In this example, signing is done with a Signer created using SignerInfo:
2486

25-
# Run the signing and verification example
26-
# In this example, signing is done with a Signer created using SignerInfo
87+
```bash
2788
python examples/sign_info.py
89+
```
90+
91+
### Run the signing and verification example
2892

29-
# Run the signing and verification example
30-
# In this example, signing is done using a callback signer
93+
In this example, signing is done using a callback signer:
94+
95+
```bash
3196
python examples/sign.py
3297
```
3398

34-
The examples will use test files from the `tests/fixtures` directory and output the results to the temporary `output` directory. Read manifest store data will be shown in the console you run the examples from.
99+

0 commit comments

Comments
 (0)