PIL Image to NSImage

20 views
Skip to first unread message

shadi abu samra

unread,
Jan 10, 2021, 2:01:13 AMJan 10
to Swift for TensorFlow
Hello,

I my case the Swift code call python function using PythonKit.The Python function return PIL image.
For now I use base64 encode/decode to convert PIL image to base64 string and from swift side convert base64 string to NSImage.But it's a slow way to get image.
Python:
buffered = BytesIO()
v.save(buffered, format="PNG")
(base64.b64encode(buffered.getvalue()).decode('ascii')) 

Swift:
NSImage(string: imageBase64)

Also I try bytesarray which convert PIL to bytes array in python side and convert python object to bytes array in swift but it's take huge cast time?
Python:
buffered = BytesIO()
v.save(buffered, format="PNG")
bytesarray(buffered.getvalue())

Swift:
[UInt8](pythonObject) ==> Takes Long time approx 14 seconds for image (2000X6000)

How can convert PIL image to NSImage? 
Please advice me

Brad Larson

unread,
Jan 11, 2021, 6:34:30 PMJan 11
to Swift for TensorFlow
Sorry about the slow reply to your other thread, I can try to answer here.

If I understand the use case: you have a 2000x6000 image that is being generated within Python code and you want to pass that off to Swift to use in an NSImage, but you're currently running into performance and memory issues?

That's a fairly large image to pass around (2000 * 6000 * 4 = 48 MB), and anything that encodes or decodes that might be pretty slow. What is your original source of this image in your Python code? Again, going to and from a PNG, as is done in the above code, is probably going to take a while to do. Further encoding that as a base64 string should make that even slower. You're going to want to find a simpler way of bridging this across.

The ideal solution would be to find a way to memory-map the backing bytes for your image to a raw byte buffer that was also accessible to Swift, so that you can avoid any expensive translation between the two. Unfortunately, I'm not as familiar with how you'd do this on the Python side (I could suggest ways to do this with memory-mapped OpenGL / Metal textures if you were working with that, for example, but that's not the case here). Others might have suggestions for how to approach this, but it might be helpful to know more about the type of image we're working with here and the source within your Python code.
Reply all
Reply to author
Forward
0 new messages