Skip to content

Commit 8a55f5a

Browse files
author
Tania Mathern
committed
fix: The exampels
1 parent d37245b commit 8a55f5a

3 files changed

Lines changed: 48 additions & 45 deletions

File tree

examples/no_thumbnails.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,29 @@ def callback_signer_es256(data: bytes) -> bytes:
8383
})
8484

8585
print("Signing image with thumbnails disabled through settings...")
86-
with c2pa.Context(settings=settings) as context:
87-
with c2pa.Signer.from_callback(
88-
callback=callback_signer_es256,
89-
alg=c2pa.C2paSigningAlg.ES256,
90-
certs=certs.decode('utf-8'),
91-
tsa_url="http://timestamp.digicert.com"
92-
) as signer:
93-
with c2pa.Builder(manifest_definition, context=context) as builder:
94-
builder.sign_file(
95-
source_path=fixtures_dir + "A.jpg",
96-
dest_path=output_dir + "A_no_thumbnail.jpg",
97-
signer=signer
98-
)
86+
context = c2pa.Context(settings=settings)
87+
with c2pa.Signer.from_callback(
88+
callback=callback_signer_es256,
89+
alg=c2pa.C2paSigningAlg.ES256,
90+
certs=certs.decode('utf-8'),
91+
tsa_url="http://timestamp.digicert.com"
92+
) as signer:
93+
with c2pa.Builder(manifest_definition, context=context) as builder:
94+
builder.sign_file(
95+
source_path=fixtures_dir + "A.jpg",
96+
dest_path=output_dir + "A_no_thumbnail.jpg",
97+
signer=signer
98+
)
9999

100-
# Read the signed image and verify no thumbnail is present.
101-
with c2pa.Reader(output_dir + "A_no_thumbnail.jpg", context=context) as reader:
102-
manifest_store = json.loads(reader.json())
103-
manifest = manifest_store["manifests"][manifest_store["active_manifest"]]
100+
# Read the signed image and verify no thumbnail is present.
101+
with c2pa.Reader(output_dir + "A_no_thumbnail.jpg", context=context) as reader:
102+
manifest_store = json.loads(reader.json())
103+
manifest = manifest_store["manifests"][manifest_store["active_manifest"]]
104104

105-
if manifest.get("thumbnail") is None:
106-
print("No thumbnail in the manifest as per settings.")
107-
else:
108-
print("Thumbnail found in the manifest.")
105+
if manifest.get("thumbnail") is None:
106+
print("No thumbnail in the manifest as per settings.")
107+
else:
108+
print("Thumbnail found in the manifest.")
109+
context.close()
109110

110111
print("\nExample completed successfully!")

examples/read.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ def read_c2pa_data(media_path: str):
3434
print(f"Reading {media_path}")
3535
try:
3636
settings = load_trust_settings()
37-
with c2pa.Context(settings=settings) as context:
38-
with c2pa.Reader(media_path, context=context) as reader:
39-
print(reader.detailed_json())
37+
context = c2pa.Context(settings=settings)
38+
with c2pa.Reader(media_path, context=context) as reader:
39+
print(reader.detailed_json())
40+
context.close()
4041
except Exception as e:
4142
print(f"Error reading C2PA data from {media_path}: {e}")
4243
sys.exit(1)

examples/sign.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,28 @@ def callback_signer_es256(data: bytes) -> bytes:
9090
print("\nSigning the image file...")
9191

9292
# Use default Context and Settings.
93-
with c2pa.Context() as context:
94-
with c2pa.Signer.from_callback(
95-
callback=callback_signer_es256,
96-
alg=c2pa.C2paSigningAlg.ES256,
97-
certs=certs.decode('utf-8'),
98-
tsa_url="http://timestamp.digicert.com"
99-
) as signer:
100-
with c2pa.Builder(manifest_definition, context=context) as builder:
101-
builder.sign_file(
102-
source_path=fixtures_dir + "A.jpg",
103-
dest_path=output_dir + "A_signed.jpg",
104-
signer=signer
105-
)
93+
context = c2pa.Context()
94+
with c2pa.Signer.from_callback(
95+
callback=callback_signer_es256,
96+
alg=c2pa.C2paSigningAlg.ES256,
97+
certs=certs.decode('utf-8'),
98+
tsa_url="http://timestamp.digicert.com"
99+
) as signer:
100+
with c2pa.Builder(manifest_definition, context=context) as builder:
101+
builder.sign_file(
102+
source_path=fixtures_dir + "A.jpg",
103+
dest_path=output_dir + "A_signed.jpg",
104+
signer=signer
105+
)
106106

107-
# Re-Read the signed image to verify
108-
print("\nReading signed image metadata:")
109-
with open(output_dir + "A_signed.jpg", "rb") as file:
110-
with c2pa.Reader("image/jpeg", file, context=context) as reader:
111-
# The validation state will depend on loaded trust settings.
112-
# Without loaded trust settings,
113-
# the manifest validation_state will be "Invalid".
114-
print(reader.json())
107+
# Re-Read the signed image to verify
108+
print("\nReading signed image metadata:")
109+
with open(output_dir + "A_signed.jpg", "rb") as file:
110+
with c2pa.Reader("image/jpeg", file, context=context) as reader:
111+
# The validation state will depend on loaded trust settings.
112+
# Without loaded trust settings,
113+
# the manifest validation_state will be "Invalid".
114+
print(reader.json())
115+
context.close()
115116

116117
print("\nExample completed successfully!")

0 commit comments

Comments
 (0)