@@ -12,7 +12,7 @@ Import the objects needed from the API:
1212from c2pa import Builder, Reader, Signer, C2paSigningAlg, C2paSignerInfo
1313```
1414
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.
1616Doing this is recommended to ensure proper resource and memory cleanup.
1717
1818## Define manifest JSON
@@ -25,13 +25,15 @@ manifest_json = json.dumps({
2525 " claim_generator" : " python_test/0.1" ,
2626 " assertions" : [
2727 {
28- " label" : " c2pa .training-mining" ,
28+ " label" : " cawg .training-mining" ,
2929 " data" : {
3030 " 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+ }
3537 }
3638 }
3739 }
6264 manifest = json.loads(reader.json())
6365 active_manifest = manifest[" manifests" ][manifest[" active_manifest" ]]
6466 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
6668 uri = active_manifest[" thumbnail" ][" identifier" ]
6769 with open (" thumbnail_v2.jpg" , " wb" ) as f:
6870 reader.resource_to_stream(uri, f)
106108 with open (" path/to/source.jpg" , " rb" ) as source_file, open (" path/to/output.jpg" , " wb" ) as dest_file:
107109 manifest_bytes = builder.sign(signer, " image/jpeg" , source_file, dest_file)
108110
109- # Verify the signed file
111+ # Verify the signed file by reading data from the signed output file
110112 with Reader(" path/to/output.jpg" ) as reader:
111113 manifest_store = json.loads(reader.json())
112114 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
126128try :
127129 # Create a reader from a format and stream
128130 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)
130132 # But in any case we need something to identify the file type
131133 with Reader(" image/jpeg" , stream) as reader:
132134 # Print manifest store as JSON, as extracted by the Reader
133135 print (" manifest store:" , reader.json())
134136
135- # Get the active manifest.
137+ # Get the active manifest
136138 manifest = json.loads(reader.json())
137139 active_manifest = manifest[" manifests" ][manifest[" active_manifest" ]]
138140 if active_manifest:
@@ -158,22 +160,23 @@ try:
158160 cert_data = cert_file.read()
159161 key_data = key_file.read()
160162
161- # Create signer info
163+ # Create signer info using the read certificate and key data
162164 signer_info = C2paSignerInfo(
163165 alg = C2paSigningAlg.PS256 ,
164166 cert = cert_data,
165167 key = key_data,
166168 timestamp_url = " http://timestamp.digicert.com"
167169 )
168170
169- # Create signer using the defined SignerInfo
171+ # Create a Signer using the SignerInfo defined previously
170172 signer = Signer.from_info(signer_info)
171173
172- # Create builder with manifest and add ingredients
174+ # Create a Builder with manifest and add ingredients
173175 with Builder(manifest_json) as builder:
174- # Add any ingredients if needed
176+ # Add any ingredients as needed
175177 with open (" path/to/ingredient.jpg" , " rb" ) as ingredient_file:
176178 ingredient_json = json.dumps({" title" : " Ingredient Image" })
179+ # Here the ingredient is added using streams
177180 builder.add_ingredient(ingredient_json, " image/jpeg" , ingredient_file)
178181
179182 # Sign using streams
182185
183186 # Verify the signed file
184187 with open (" path/to/output.jpg" , " rb" ) as stream:
188+ # Create a Reader to read data
185189 with Reader(" image/jpeg" , stream) as reader:
186190 manifest_store = json.loads(reader.json())
187191 active_manifest = manifest_store[" manifests" ][manifest_store[" active_manifest" ]]
0 commit comments