Skip to content

Commit c71425c

Browse files
authored
Merge pull request #24 from contentauth/ok-nick/stream-examples
Add stream examples to README
2 parents ccbb35a + 48f7f23 commit c71425c

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Import the API as follows:
3636
from c2pa import *
3737
```
3838

39-
### Read and validate C2PA data in a file
39+
### Read and validate C2PA data in a file or stream
4040

4141
Use the `Reader` to read C2PA data from the specified file.
4242
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).
@@ -48,7 +48,12 @@ The manifests may contain binary resources such as thumbnails which can be retri
4848
NOTE: For a comprehensive reference to the JSON manifest structure, see the [Manifest store reference](https://opensource.contentauthenticity.org/docs/manifest/manifest-ref).
4949
```py
5050
try:
51-
reader = c2pa.Reader("path/to/media_file.jpg")
51+
# Create a reader from a file path
52+
reader = c2pa.Reader.from_file("path/to/media_file.jpg")
53+
# 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)
5257

5358
# Print the JSON for a manifest.
5459
print("manifest store:", reader.json())
@@ -65,7 +70,7 @@ except Exception as err:
6570
print(err)
6671
```
6772

68-
### Add a signed manifest to a media file
73+
### Add a signed manifest to a media file or stream
6974

7075
Use a `Builder` to add a manifest to an asset.
7176

@@ -115,6 +120,10 @@ try:
115120
# The uri provided here "thumbnail" must match an identifier in the manifest definition.
116121
builder.add_resource_file("thumbnail", "tests/fixtures/A_thumbnail.jpg")
117122

123+
# Or add the resource from a stream
124+
a_thumbnail_jpg_stream = open("tests/fixtures/A_thumbnail.jpg", "rb")
125+
builder.add_resource("image/jpeg", a_thumbnail_jpg_stream)
126+
118127
# Define an ingredient, in this case a parent ingredient named A.jpg, with a thumbnail
119128
ingredient_json = {
120129
"title": "A.jpg",
@@ -128,6 +137,10 @@ try:
128137
# Add the ingredient to the builder loading information from a source file.
129138
builder.add_ingredient_file(ingredient_json, "tests/fixtures/A.jpg")
130139

140+
# Or add the ingredient from a stream
141+
a_jpg_stream = open("tests/fixtures/A.jpg", "rb")
142+
builder.add_ingredient("image/jpeg", a_jpg_stream)
143+
131144
# At this point we could archive or unarchive our Builder to continue later.
132145
# In this example we use a bytearray for the archive stream.
133146
# all ingredients and resources will be saved in the archive
@@ -140,6 +153,11 @@ try:
140153
# This returns the binary manifest data that could be uploaded to cloud storage.
141154
c2pa_data = builder.sign_file(signer, "tests/fixtures/A.jpg", "target/out.jpg")
142155

156+
# Or sign the builder with a stream and output it to a stream
157+
input_stream = open("tests/fixtures/A.jpg", "rb")
158+
output_stream = open("target/out.jpg", "wb")
159+
c2pa_data = builder.sign(signer, "image/jpeg", input_stream, output_stream)
160+
143161
except Exception as err:
144162
print(err)
145163
```

0 commit comments

Comments
 (0)