@@ -12,7 +12,7 @@ Import the objects needed from the API:
12
12
from c2pa import Builder, Reader, Signer, C2paSigningAlg, C2paSignerInfo
13
13
```
14
14
15
- You can use both ` Builder ` and ` Reader ` classes with context managers by using a ` with ` statement.
15
+ You can use both ` Builder ` , ` Reader ` and ` Signer ` classes with context managers by using a ` with ` statement.
16
16
Doing this is recommended to ensure proper resource and memory cleanup.
17
17
18
18
## Define manifest JSON
@@ -25,13 +25,15 @@ manifest_json = json.dumps({
25
25
" claim_generator" : " python_test/0.1" ,
26
26
" assertions" : [
27
27
{
28
- " label" : " c2pa .training-mining" ,
28
+ " label" : " cawg .training-mining" ,
29
29
" data" : {
30
30
" entries" : {
31
- " c2pa.ai_generative_training" : { " use" : " notAllowed" },
32
- " c2pa.ai_inference" : { " use" : " notAllowed" },
33
- " c2pa.ai_training" : { " use" : " notAllowed" },
34
- " c2pa.data_mining" : { " use" : " notAllowed" }
31
+ " cawg.ai_inference" : {
32
+ " use" : " notAllowed"
33
+ },
34
+ " cawg.ai_generative_training" : {
35
+ " use" : " notAllowed"
36
+ }
35
37
}
36
38
}
37
39
}
62
64
manifest = json.loads(reader.json())
63
65
active_manifest = manifest[" manifests" ][manifest[" active_manifest" ]]
64
66
if active_manifest:
65
- # get the uri to the manifest's thumbnail and write it to a file
67
+ # Get the uri to the manifest's thumbnail and write it to a file
66
68
uri = active_manifest[" thumbnail" ][" identifier" ]
67
69
with open (" thumbnail_v2.jpg" , " wb" ) as f:
68
70
reader.resource_to_stream(uri, f)
106
108
with open (" path/to/source.jpg" , " rb" ) as source_file, open (" path/to/output.jpg" , " wb" ) as dest_file:
107
109
manifest_bytes = builder.sign(signer, " image/jpeg" , source_file, dest_file)
108
110
109
- # Verify the signed file
111
+ # Verify the signed file by reading data from the signed output file
110
112
with Reader(" path/to/output.jpg" ) as reader:
111
113
manifest_store = json.loads(reader.json())
112
114
active_manifest = manifest_store[" manifests" ][manifest_store[" active_manifest" ]]
@@ -126,13 +128,13 @@ Instead of working with files, you can read, validate, and add a signed manifest
126
128
try :
127
129
# Create a reader from a format and stream
128
130
with open (" path/to/media_file.jpg" , " rb" ) as stream:
129
- # First parameter can be mimetype or extension of the file
131
+ # First parameter should be the type of the file (here, we use the mimetype)
130
132
# But in any case we need something to identify the file type
131
133
with Reader(" image/jpeg" , stream) as reader:
132
134
# Print manifest store as JSON, as extracted by the Reader
133
135
print (" manifest store:" , reader.json())
134
136
135
- # Get the active manifest.
137
+ # Get the active manifest
136
138
manifest = json.loads(reader.json())
137
139
active_manifest = manifest[" manifests" ][manifest[" active_manifest" ]]
138
140
if active_manifest:
@@ -158,22 +160,23 @@ try:
158
160
cert_data = cert_file.read()
159
161
key_data = key_file.read()
160
162
161
- # Create signer info
163
+ # Create signer info using the read certificate and key data
162
164
signer_info = C2paSignerInfo(
163
165
alg = C2paSigningAlg.PS256 ,
164
166
cert = cert_data,
165
167
key = key_data,
166
168
timestamp_url = " http://timestamp.digicert.com"
167
169
)
168
170
169
- # Create signer using the defined SignerInfo
171
+ # Create a Signer using the SignerInfo defined previously
170
172
signer = Signer.from_info(signer_info)
171
173
172
- # Create builder with manifest and add ingredients
174
+ # Create a Builder with manifest and add ingredients
173
175
with Builder(manifest_json) as builder:
174
- # Add any ingredients if needed
176
+ # Add any ingredients as needed
175
177
with open (" path/to/ingredient.jpg" , " rb" ) as ingredient_file:
176
178
ingredient_json = json.dumps({" title" : " Ingredient Image" })
179
+ # Here the ingredient is added using streams
177
180
builder.add_ingredient(ingredient_json, " image/jpeg" , ingredient_file)
178
181
179
182
# Sign using streams
182
185
183
186
# Verify the signed file
184
187
with open (" path/to/output.jpg" , " rb" ) as stream:
188
+ # Create a Reader to read data
185
189
with Reader(" image/jpeg" , stream) as reader:
186
190
manifest_store = json.loads(reader.json())
187
191
active_manifest = manifest_store[" manifests" ][manifest_store[" active_manifest" ]]
0 commit comments