Skip to content

Commit

Permalink
new_from_source keeps a ref to the source
Browse files Browse the repository at this point in the history
Since it might be a custom source with callabcks, and the python wrapper
for the source class must stay alive.

Added another example of custom sources.
  • Loading branch information
jcupitt committed Feb 19, 2024
1 parent 4d5dfa9 commit 3eff2c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

- ensure compatibility with a single shared libvips library [kleisauke]
- add flags_dict(), enum_dict() for better flags introspection
- improve generation of enums.py
- improve generation of `enums.py`
- add `stream.py` example
- fix a missing reference issue with custom sources

## Version 2.2.2 (released 4 Jan 2023)

Expand Down
27 changes: 27 additions & 0 deletions examples/stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3

import sys
import gc
import requests
import pyvips

URL = "https://cdn.filestackcontent.com/bnTGtQw5ShqMPpxH2tMw"
URLS = [URL] * int(sys.argv[1])

session = requests.Session()

image = pyvips.Image.black(1500, 1500)

for i, url in enumerate(URLS):
print(f"loading {url} ...")
stream = session.get(url, stream=True).raw

source = pyvips.SourceCustom()
source.on_read((lambda stream: stream.read)(stream))

tile = pyvips.Image.new_from_source(source, "", access="sequential")
image = image.composite2(tile, "over", x= 50 * (i + 1), y= 50 * (i + 1))

print(f"writing output.jpg ...")
image.write_to_file("output.jpg")

11 changes: 9 additions & 2 deletions pyvips/vimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,15 @@ def new_from_source(source, options, **kwargs):
raise Error('unable to load from source')
name = _to_string(pointer)

return pyvips.Operation.call(name, source,
string_options=options, **kwargs)
image = pyvips.Operation.call(name, source,
string_options=options, **kwargs)

# keep a secret ref to the source object .. we need that to stay
# alive, since it might be a custom source that triggers a python
# callback
image._references.append(source)

return image

@staticmethod
def new_temp_file(format):
Expand Down

0 comments on commit 3eff2c7

Please sign in to comment.