MemoryView to swift

29 views
Skip to first unread message

shadi abu samra

unread,
Jan 7, 2021, 7:21:20 AM1/7/21
to Swift for TensorFlow
Hello,
How can convert memoryview object to swift NSImage?
Below python func return memoryview from image and I get it as a "memory type" in python kit.So I need to return memoryview to NSImage

 if isinstance(v, Image.Image):
     buffered = BytesIO()
     v.save(buffered, format="PNG")

return memoryview(buffered.getvalue())

Please advice me

Brad Larson

unread,
Jan 7, 2021, 1:07:27 PM1/7/21
to Swift for TensorFlow
From the code snippet you've provided, it looks like you're trying to encode a Python Image (from PIL or related?) to PNG bytes, and then load those into an NSImage. To do this literal operation, you'd need to get the bytes contained within the MemoryView instance, create a Swift Data instance from them, and then use NSImage(data:) to initialize an NSImage from those bytes.

However, going to and from a PNG representation might not be the fastest way to perform this conversion. You may want to start from the raw bytes of your image and create an NSImage from that (via CGImage), which will save on the intermediate step.

Sorry for being a little vague about this, because depending on what exactly you're attempting there are possibly multiple other paths to accomplish this with various performance and memory usage characteristics.
Message has been deleted

shadi abu samra

unread,
Jan 7, 2021, 1:27:35 PM1/7/21
to Swift for TensorFlow, bradl...@google.com
Thank you.

Simply I have python function which return PIL image and need to get PIL image as NSImage from swift side.
Now I convert PIL image to base64 and from swift side convert base64 to NSImage but it's consume large memory. So I try to find another way to get image either change it to bytes array or memory view.
Please I you have a way to do this help me

shadi abu samra

unread,
Jan 8, 2021, 8:30:57 PM1/8/21
to Swift for TensorFlow, bradl...@google.com
Any idea?

--
To unsubscribe from this group and stop receiving emails from it, send an email to swift+un...@tensorflow.org.
--
Regards Shadi
Reply all
Reply to author
Forward
0 new messages