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
Copy file name to clipboardExpand all lines: README.md
+5-322Lines changed: 5 additions & 322 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,10 @@ It enables you to read and validate C2PA manifest data from and add signed manif
9
9
10
10
<divstyle={{display:'none'}}>
11
11
12
-
For information on what's in the current release, see the [Release notes](docs/release-notes.md).
12
+
Additional documentation:
13
+
-[Using the Python library](docs/usage.md)
14
+
-[Release notes](docs/release-notes.md)
15
+
-[Contributing to the project](docs/project-contributions.md)
13
16
14
17
</div>
15
18
@@ -21,7 +24,7 @@ Install from PyPI by entering this command:
21
24
pip install -U c2pa-python
22
25
```
23
26
24
-
This is a platform wheel built with Rust that works on Windows, macOS, and most Linux distributions (using [manylinux](https://github.com/pypa/manylinux)). If you need to run on another platform, see [Development](#development) for information on how to build from source.
27
+
This is a platform wheel built with Rust that works on Windows, macOS, and most Linux distributions (using [manylinux](https://github.com/pypa/manylinux)). If you need to run on another platform, see [Project contributions - Development](docs/project-contributions.md#development) for information on how to build from source.
The Python library [supports the same media file formats](https://github.com/contentauth/c2pa-rs/blob/main/docs/supported-formats.md) as the Rust library.
47
50
48
-
## Usage
49
-
50
-
This package works with media files in the [supported formats](https://github.com/contentauth/c2pa-rs/blob/main/docs/supported-formats.md).
51
-
52
-
### Import
53
-
54
-
Import the API as follows:
55
-
56
-
```py
57
-
from c2pa import*
58
-
```
59
-
60
-
### Define manifest JSON
61
-
62
-
The Python library works with both file-based and stream-based operations.
63
-
In both cases, the manifest JSON string defines the C2PA manifest to add to an asset; for example:
The `sign_ps256` function is [defined in the library](https://github.com/contentauth/c2pa-python/blob/main/c2pa/c2pa_api/c2pa_api.py#L209) is used in both file-based and stream-based methods and is reproduced here to show how signing is performed.
87
-
88
-
```py
89
-
# Example of using Python crypto to sign data using openssl with Ps256
90
-
from cryptography.hazmat.primitives import hashes, serialization
91
-
from cryptography.hazmat.primitives.asymmetric import padding
**Read and validate C2PA data from an asset file**
113
-
114
-
Use the `Reader` to read C2PA data from the specified asset file.
115
-
116
-
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.
117
-
118
-
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`.
119
-
120
-
NOTE: For a comprehensive reference to the JSON manifest structure, see the [Manifest store reference](https://opensource.contentauthenticity.org/docs/manifest/manifest-ref).
# get the uri to the manifest's thumbnail and write it to a file
135
-
uri = manifest["thumbnail"]["identifier"]
136
-
reader.resource_to_file(uri, "thumbnail_v2.jpg")
137
-
138
-
exceptExceptionas err:
139
-
print(err)
140
-
```
141
-
142
-
**Add a signed manifest to an asset file**
143
-
144
-
**WARNING**: This example accesses the private key and security certificate directly from the local file system. This is fine during development, but doing so in production may be insecure. Instead use a Key Management Service (KMS) or a hardware security module (HSM) to access the certificate and key; for example as show in the [C2PA Python Example](https://github.com/contentauth/c2pa-python-example).
145
-
146
-
Use a `Builder` to add a manifest to an asset:
147
-
148
-
```py
149
-
try:
150
-
# Define a function to sign the claim bytes
151
-
# In this case we are using a pre-defined sign_ps256 method, passing in our private cert
152
-
# Normally this cert would be kept safe in some other location
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.
200
-
201
-
**Read and validate C2PA data from a stream**
202
-
203
-
```py
204
-
try:
205
-
# It's also possible to create a reader from a format and stream
206
-
# Note that these two readers are functionally equivalent
207
-
stream =open("path/to/media_file.jpg", "rb")
208
-
reader = c2pa.Reader("image/jpeg", stream)
209
-
210
-
# Print the JSON for a manifest.
211
-
print("manifest store:", reader.json())
212
-
213
-
# Get the active manifest.
214
-
manifest = reader.get_active_manifest()
215
-
if manifest !=None:
216
-
217
-
# get the uri to the manifest's thumbnail and write it to a file
218
-
uri = manifest["thumbnail"]["identifier"]
219
-
reader.resource_to_file(uri, "thumbnail_v2.jpg")
220
-
221
-
exceptExceptionas err:
222
-
print(err)
223
-
```
224
-
225
-
**Add a signed manifest to a stream**
226
-
227
-
**WARNING**: This example accesses the private key and security certificate directly from the local file system. This is fine during development, but doing so in production may be insecure. Instead use a Key Management Service (KMS) or a hardware security module (HSM) to access the certificate and key; for example as show in the [C2PA Python Example](https://github.com/contentauth/c2pa-python-example).
228
-
229
-
Use a `Builder` to add a manifest to an asset:
230
-
231
-
```py
232
-
try:
233
-
# Define a function to sign the claim bytes
234
-
# In this case we are using a pre-defined sign_ps256 method, passing in our private cert
235
-
# Normally this cert would be kept safe in some other location
3. Setup the virtual environment with local changes: `maturin develop`
356
-
4. Run the tests: `pytest`
357
-
5. Deactivate the virtual environment: `deactivate`
358
-
359
-
For example:
360
-
361
-
```bash
362
-
source .venv/bin/activate
363
-
maturin develop
364
-
python3 tests/training.py
365
-
deactivate
366
-
```
367
-
368
51
## License
369
52
370
53
This package is distributed under the terms of both the [MIT license](https://github.com/contentauth/c2pa-python/blob/main/LICENSE-MIT) and the [Apache License (Version 2.0)](https://github.com/contentauth/c2pa-python/blob/main/LICENSE-APACHE).
0 commit comments