You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package works with media files in the [supported formats](https://github.com/contentauth/c2pa-rs/blob/main/docs/supported-formats.md).
4
4
5
+
For complete working examples, see the [examples folder](https://github.com/contentauth/c2pa-python/tree/main/examples) in the repository.
6
+
5
7
## Import
6
8
7
-
Import the API as follows:
9
+
Import the objects needed from the API:
8
10
9
11
```py
10
-
from c2pa import*
12
+
from c2pa importBuilder, Reader, Signer, C2paSigningAlg, C2paSignerInfo
11
13
```
12
14
15
+
You can use both `Builder` and `Reader` classes with context managers by using a `with` statement.
16
+
Doing this is recommended to ensure proper resource and memory cleanup.
17
+
13
18
## Define manifest JSON
14
19
15
20
The Python library works with both file-based and stream-based operations.
16
-
In both cases, the manifest JSON string defines the C2PA manifest to add to an asset; for example:
21
+
In both cases, the manifest JSON string defines the C2PA manifest to add to an asset. For example:
17
22
18
23
```py
19
24
manifest_json = json.dumps({
@@ -34,58 +39,33 @@ manifest_json = json.dumps({
34
39
})
35
40
```
36
41
37
-
## Signing function
38
-
39
-
The `sign_ps256` function is [defined in the library](https://github.com/contentauth/c2pa-python/blob/main/c2pa/c2pa_api/c2pa_api.py#L244) and used in both file-based and stream-based methods. It's reproduced here to show how signing is performed.
40
-
41
-
```py
42
-
# Example of using Python crypto to sign data using openssl with Ps256
43
-
from cryptography.hazmat.primitives import hashes, serialization
44
-
from cryptography.hazmat.primitives.asymmetric import padding
45
-
46
-
defsign_ps256(data: bytes, key: bytes) -> bytes:
47
-
private_key = serialization.load_pem_private_key(
48
-
key,
49
-
password=None,
50
-
)
51
-
signature = private_key.sign(
52
-
data,
53
-
padding.PSS(
54
-
mgf=padding.MGF1(hashes.SHA256()),
55
-
salt_length=padding.PSS.MAX_LENGTH
56
-
),
57
-
hashes.SHA256()
58
-
)
59
-
return signature
60
-
```
61
-
62
42
## File-based operation
63
43
64
44
### Read and validate C2PA data
65
45
66
-
Use the `Reader` to read C2PA data from the specified asset file.
46
+
Use the `Reader` to read C2PA data from the specified asset file.
67
47
68
48
This examines the specified media file for C2PA data and generates a report of any data it finds. If there are validation errors, the report includes a `validation_status` field.
69
49
70
-
An asset file may contain many manifests in a manifest store. The most recent manifest is identified by the value of the `active_manifest` field in the manifests map. The manifests may contain binary resources such as thumbnails which can be retrieved with `resource_to_stream`or `resource_to_file`using the associated `identifier` field values and a `uri`.
50
+
An asset file may contain many manifests in a manifest store. The most recent manifest is identified by the value of the `active_manifest` field in the manifests map. The manifests may contain binary resources such as thumbnails which can be retrieved with `resource_to_stream` using the associated `identifier` field values and a `uri`.
71
51
72
52
NOTE: For a comprehensive reference to the JSON manifest structure, see the [Manifest store reference](https://opensource.contentauthenticity.org/docs/manifest/manifest-ref).
Instead of working with files, you can read, validate, and add a signed manifest to streamed data. This example code does the same thing as the file-based example.
121
+
Instead of working with files, you can read, validate, and add a signed manifest to streamed data. This example is similar to what the file-based example does.
151
122
152
-
### Read and validate C2PA data
123
+
### Read and validate C2PA data using streams
153
124
154
125
```py
155
126
try:
156
-
# It's also possible to create a reader from a format and stream
157
-
# Note that these two readers are functionally equivalent
158
-
stream =open("path/to/media_file.jpg", "rb")
159
-
reader = c2pa.Reader("image/jpeg", stream)
160
-
161
-
# Print the JSON for a manifest.
162
-
print("manifest store:", reader.json())
163
-
164
-
# Get the active manifest.
165
-
manifest = reader.get_active_manifest()
166
-
if manifest !=None:
167
-
168
-
# get the uri to the manifest's thumbnail and write it to a file
169
-
uri = manifest["thumbnail"]["identifier"]
170
-
reader.resource_to_file(uri, "thumbnail_v2.jpg")
127
+
# Create a reader from a format and stream
128
+
withopen("path/to/media_file.jpg", "rb") as stream:
129
+
# First parameter can be mimetype or extension of the file
130
+
# But in any case we need something to identify the file type
131
+
with Reader("image/jpeg", stream) as reader:
132
+
# Print manifest store as JSON, as extracted by the Reader
The `examples/training.py` script demonstrates how to add a "Do Not Train" assertion to an asset and verify it.
8
+
9
+
### Signing and Verifying Assets
10
+
11
+
The `examples/sign.py` script shows how to sign an asset with a C2PA manifest and verify it.
12
+
13
+
## Running the Examples
14
+
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).
16
+
17
+
Then you can run the examples with the following commands:
18
+
19
+
```bash
20
+
# Run the "Do Not Train" assertion example
21
+
python examples/training.py
22
+
23
+
# Run the signing and verification example
24
+
python examples/sign.py
25
+
```
26
+
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.
0 commit comments