|
1 |
| -# Usage examples |
| 1 | +# Python example code |
2 | 2 |
|
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. |
4 | 5 |
|
5 |
| -### Adding a "Do Not Train" Assertion |
| 6 | +## Signing and verifying assets |
6 | 7 |
|
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. |
8 | 9 |
|
9 |
| -### Signing and Verifying Assets |
10 | 10 |
|
11 |
| -The `examples/sign.py` script shows how to sign an asset with a C2PA manifest and verify it. |
| 11 | +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`: |
12 | 12 |
|
13 |
| -## Running the Examples |
| 13 | +```py |
| 14 | +manifest_definition = { |
| 15 | + // ... JSON omitted here |
| 16 | +} |
14 | 17 |
|
15 |
| -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). |
| 18 | +builder = c2pa.Builder(manifest_definition) |
16 | 19 |
|
17 |
| -Then you can run the examples with the following commands: |
| 20 | +with open(fixtures_dir + "C.jpg", "rb") as source: |
| 21 | + with open(output_dir + "C_signed.jpg", "wb") as dest: |
| 22 | + result = builder.sign(signer, "image/jpeg", source, dest) |
| 23 | +``` |
| 24 | + |
| 25 | +Then these statements read and verify the signed asset: |
| 26 | + |
| 27 | +```py |
| 28 | +print("\nReading signed image metadata:") |
| 29 | +with open(output_dir + "C_signed.jpg", "rb") as file: |
| 30 | + reader = c2pa.Reader("image/jpeg", file) |
| 31 | + print(reader.json()) |
| 32 | +``` |
| 33 | + |
| 34 | +## Adding a "do not train" assertion |
| 35 | + |
| 36 | +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. |
| 37 | + |
| 38 | +These statements sign the asset using a stream: |
| 39 | + |
| 40 | +```py |
| 41 | + with open(testFile, "rb") as source_file: |
| 42 | + with open(testOutputFile, "wb") as dest_file: |
| 43 | + result = builder.sign(signer, "image/jpeg", source_file, dest_file) |
| 44 | +``` |
| 45 | + |
| 46 | +These statements verify the asset and check its attached manifest for a "do not train" assertion: |
| 47 | + |
| 48 | +```py |
| 49 | +allowed = True # opt out model, assume training is ok if the assertion doesn't exist |
| 50 | +try: |
| 51 | + # Create reader using the current API |
| 52 | + reader = c2pa.Reader(testOutputFile) |
| 53 | + manifest_store = json.loads(reader.json()) |
| 54 | + |
| 55 | + manifest = manifest_store["manifests"][manifest_store["active_manifest"]] |
| 56 | + for assertion in manifest["assertions"]: |
| 57 | + if assertion["label"] == "c2pa.training-mining": |
| 58 | + if getitem(assertion, ("data","entries","c2pa.ai_training","use")) == "notAllowed": |
| 59 | + allowed = False |
| 60 | + |
| 61 | + # get the ingredient thumbnail and save it to a file using resource_to_stream |
| 62 | + uri = getitem(manifest,("ingredients", 0, "thumbnail", "identifier")) |
| 63 | + with open(output_dir + "thumbnail_v2.jpg", "wb") as thumbnail_output: |
| 64 | + reader.resource_to_stream(uri, thumbnail_output) |
| 65 | + |
| 66 | +except Exception as err: |
| 67 | + sys.exit(err) |
| 68 | +``` |
| 69 | + |
| 70 | +## Running the examples |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +Run the "do not train" assertion example: |
18 | 75 |
|
19 | 76 | ```bash
|
20 |
| -# Run the "Do Not Train" assertion example |
21 | 77 | python examples/training.py
|
| 78 | +``` |
| 79 | + |
| 80 | +Run the signing and verification example: |
22 | 81 |
|
23 |
| -# Run the signing and verification example |
| 82 | +```bash |
24 | 83 | python examples/sign.py
|
25 | 84 | ```
|
26 | 85 |
|
27 |
| -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. |
| 86 | + |
0 commit comments