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
Python bindings for the C2PA Content Authenticity Initiative (CAI) library.
3
+
This repository implements Python bindings for the Content Authenticity Initiative (CAI) library.
4
4
5
5
This library enables you to read and validate C2PA data in supported media files and add signed manifests to supported media files.
6
6
7
-
**NOTE**: This is a completely different API from 0.4.0. Check[Release notes](#release-notes) for changes.
7
+
**NOTE**: Starting with version 0.5.0, this package has a completely different API from version 0.4.0. See[Release notes](#release-notes) for more information.
8
8
9
9
**WARNING**: This is an prerelease version of this library. There may be bugs and unimplemented features, and the API is subject to change.
10
10
@@ -18,6 +18,16 @@ pip install -U c2pa-python
18
18
19
19
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.
20
20
21
+
### Updating
22
+
23
+
Determine what version you've got by entering this command:
24
+
25
+
```
26
+
pip list | grep c2pa-python
27
+
```
28
+
29
+
If the version shown is lower than the most recent version, then update by [reinstalling](#installation).
30
+
21
31
### Reinstalling
22
32
23
33
If you tried unsuccessfully to install this package before the [0.40 release](https://github.com/contentauth/c2pa-python/releases/tag/v0.4), then use this command to reinstall:
@@ -36,24 +46,72 @@ Import the API as follows:
36
46
from c2pa import*
37
47
```
38
48
39
-
### Read and validate C2PA data in a file or stream
49
+
### Define manifest JSON
40
50
41
-
Use the `Reader` to read C2PA data from the specified file.
42
-
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. For a summary of supported media types, see [Supported file formats](#supported-file-formats).
51
+
The Python library works with both file-based and stream-based operations.
52
+
In both cases, the manifest JSON string defines the C2PA manifest to add to an asset; for example:
43
53
44
-
A media 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 `sign_ps256` function is [defined in the library](https://github.com/contentauth/c2pa-python/blob/main/c2pa/c2pa_api/c2pa_api.py#L209) and is reproduced here to show how signing is performed.
76
+
77
+
```py
78
+
# Example of using Python crypto to sign data using openssl with Ps256
79
+
from cryptography.hazmat.primitives import hashes, serialization
80
+
from cryptography.hazmat.primitives.asymmetric import padding
**Read and validate C2PA data from an asset file**
102
+
103
+
Use the `Reader` to read C2PA data from the specified asset file (see [supported file formats](#supported-file-formats)).
104
+
105
+
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.
45
106
46
-
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`.
107
+
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
108
48
109
NOTE: For a comprehensive reference to the JSON manifest structure, see the [Manifest store reference](https://opensource.contentauthenticity.org/docs/manifest/manifest-ref).
# It's also possible to create a reader from a format and stream
54
-
# Note that these two readers are functionally equivalent
55
-
stream =open("path/to/media_file.jpg", "rb")
56
-
reader = c2pa.Reader("image/jpeg", stream)
57
115
58
116
# Print the JSON for a manifest.
59
117
print("manifest store:", reader.json())
@@ -70,9 +128,9 @@ except Exception as err:
70
128
print(err)
71
129
```
72
130
73
-
### Add a signed manifest to a media file or stream
131
+
**Add a signed manifest to an asset file**
74
132
75
-
**WARNING**: This example accesses the private key and security certficate 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).
133
+
**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).
76
134
77
135
Use a `Builder` to add a manifest to an asset:
78
136
@@ -90,42 +148,12 @@ try:
90
148
# Create a signer from the private signer, certs and a time stamp service url
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.
189
+
190
+
**Read and validate C2PA data from a stream**
191
+
192
+
```py
193
+
try:
194
+
# It's also possible to create a reader from a format and stream
195
+
# Note that these two readers are functionally equivalent
196
+
stream =open("path/to/media_file.jpg", "rb")
197
+
reader = c2pa.Reader("image/jpeg", stream)
198
+
199
+
# Print the JSON for a manifest.
200
+
print("manifest store:", reader.json())
201
+
202
+
# Get the active manifest.
203
+
manifest = reader.get_active_manifest()
204
+
if manifest !=None:
205
+
206
+
# get the uri to the manifest's thumbnail and write it to a file
207
+
uri = manifest["thumbnail"]["identifier"]
208
+
reader.resource_to_file(uri, "thumbnail_v2.jpg")
162
209
163
210
exceptExceptionas err:
164
211
print(err)
165
-
```
212
+
```
213
+
214
+
**Add a signed manifest to a stream**
166
215
167
-
### Creating a manifest JSON definition file
216
+
**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).
168
217
169
-
The manifest JSON string defines the C2PA manifest to add to the file.
0 commit comments