|
| 1 | +# Copyright 2025 Adobe. All rights reserved. |
| 2 | +# This file is licensed to you under the Apache License, |
| 3 | +# Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| 4 | +# or the MIT license (http://opensource.org/licenses/MIT), |
| 5 | +# at your option. |
| 6 | +# Unless required by applicable law or agreed to in writing, |
| 7 | +# this software is distributed on an "AS IS" BASIS, WITHOUT |
| 8 | +# WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or |
| 9 | +# implied. See the LICENSE-MIT and LICENSE-APACHE files for the |
| 10 | +# specific language governing permissions and limitations under |
| 11 | +# each license. |
| 12 | + |
| 13 | +# This example shows how to sign an image with a C2PA manifest |
| 14 | +# and read the metadata added to the image. |
| 15 | + |
| 16 | +import os |
| 17 | +import c2pa |
| 18 | + |
| 19 | +fixtures_dir = os.path.join(os.path.dirname(__file__), "../tests/fixtures/") |
| 20 | +output_dir = os.path.join(os.path.dirname(__file__), "../output/") |
| 21 | + |
| 22 | +# ensure the output directory exists |
| 23 | +if not os.path.exists(output_dir): |
| 24 | + os.makedirs(output_dir) |
| 25 | + |
| 26 | +print("c2pa version:") |
| 27 | +version = c2pa.sdk_version() |
| 28 | +print(version) |
| 29 | + |
| 30 | +# Read existing C2PA metadata from the file |
| 31 | +print("\nReading existing C2PA metadata:") |
| 32 | +with open(fixtures_dir + "C.jpg", "rb") as file: |
| 33 | + reader = c2pa.Reader("image/jpeg", file) |
| 34 | + print(reader.json()) |
| 35 | + |
| 36 | +# Create a signer from certificate and key files |
| 37 | +certs = open(fixtures_dir + "es256_certs.pem", "rb").read() |
| 38 | +key = open(fixtures_dir + "es256_private.key", "rb").read() |
| 39 | + |
| 40 | +signer_info = c2pa.C2paSignerInfo( |
| 41 | + alg=b"es256", # Use bytes instead of encoded string |
| 42 | + sign_cert=certs, |
| 43 | + private_key=key, |
| 44 | + ta_url=b"http://timestamp.digicert.com" # Use bytes and add timestamp URL |
| 45 | +) |
| 46 | + |
| 47 | +signer = c2pa.Signer.from_info(signer_info) |
| 48 | + |
| 49 | +# Create a manifest definition as a dictionary |
| 50 | +manifest_definition = { |
| 51 | + "claim_generator": "python_example", |
| 52 | + "claim_generator_info": [{ |
| 53 | + "name": "python_example", |
| 54 | + "version": "0.0.1", |
| 55 | + }], |
| 56 | + "format": "image/jpeg", |
| 57 | + "title": "Python Example Image", |
| 58 | + "ingredients": [], |
| 59 | + "assertions": [ |
| 60 | + { |
| 61 | + 'label': 'stds.schema-org.CreativeWork', |
| 62 | + 'data': { |
| 63 | + '@context': 'http://schema.org/', |
| 64 | + '@type': 'CreativeWork', |
| 65 | + 'author': [ |
| 66 | + {'@type': 'Person', 'name': 'Example User'} |
| 67 | + ] |
| 68 | + }, |
| 69 | + 'kind': 'Json' |
| 70 | + } |
| 71 | + ] |
| 72 | +} |
| 73 | + |
| 74 | +builder = c2pa.Builder(manifest_definition) |
| 75 | + |
| 76 | +# Sign the image |
| 77 | +print("\nSigning the image...") |
| 78 | +with open(fixtures_dir + "C.jpg", "rb") as source: |
| 79 | + with open(output_dir + "C_signed.jpg", "wb") as dest: |
| 80 | + result = builder.sign(signer, "image/jpeg", source, dest) |
| 81 | + |
| 82 | +# Read the signed image to verify |
| 83 | +print("\nReading signed image metadata:") |
| 84 | +with open(output_dir + "C_signed.jpg", "rb") as file: |
| 85 | + reader = c2pa.Reader("image/jpeg", file) |
| 86 | + print(reader.json()) |
| 87 | + |
| 88 | +print("\nExample completed successfully!") |
| 89 | + |
0 commit comments