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
+
> **Note**: 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 API:
8
10
9
11
```py
10
-
from c2pa import*
12
+
from c2pa importBuilder, Reader, Signer, C2paSigningAlg, C2paSignerInfo
11
13
```
12
14
13
15
## Define manifest JSON
14
16
15
17
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:
18
+
In both cases, the manifest JSON string defines the C2PA manifest to add to an asset. For example:
17
19
18
20
```py
19
21
manifest_json = json.dumps({
@@ -34,58 +36,33 @@ manifest_json = json.dumps({
34
36
})
35
37
```
36
38
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
39
## File-based operation
63
40
64
41
### Read and validate C2PA data
65
42
66
-
Use the `Reader` to read C2PA data from the specified asset file.
43
+
Use the `Reader` to read C2PA data from the specified asset file.
67
44
68
45
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
46
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`.
47
+
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
48
72
49
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.
151
119
152
-
### Read and validate C2PA data
120
+
### Read and validate C2PA data using streams
153
121
154
122
```py
155
123
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")
124
+
# Create a reader from a format and stream
125
+
withopen("path/to/media_file.jpg", "rb") as stream:
126
+
# First parameter can be mimetype or extension of the file
127
+
# But in any case we need something to identify the file type
The `examples/training.py` script demonstrates how to add a "Do Not Train" assertion to an asset and verify it.
6
+
7
+
### Signing and Verifying Assets
8
+
9
+
The `examples/sign.py` script shows how to sign an asset with a C2PA manifest and verify it.
10
+
11
+
## Running the Examples
12
+
13
+
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).
14
+
15
+
Then you can run the examples with the following commands:
16
+
17
+
```bash
18
+
# Run the "Do Not Train" assertion example
19
+
python examples/training.py
20
+
21
+
# Run the signing and verification example
22
+
python examples/sign.py
23
+
```
24
+
25
+
The examples will use test files from the `tests/fixtures` directory and output the results to the `output` directory. Read manifest store data will be shown in the console you run the examples from.
0 commit comments