Suggestions for decoding/encoding bytes for .png
Closed this issue · 1 comments
I am interested in creating a Foundry app that allows me to READ/WRITE .png images to a DATASET. At present I am satisfied with testing READ using the Palantir SDK. I have started this testing by creating a unstructured DATASET on Foundry and added a simple .png.
I have had great success in my ability to call the DATASET down from Foundry, but I am left a little confused with how to decode the bytes (?) that are returned?
Any suggestions for next steps? Ideally Id like to convert this into an array format that can be displayed using an image processing library like Matplotlib. It would also be useful to know how to encode this data for future WRITE capabilities. Any chance that some level of encoding/decoding could be added during this dataset
call for .png? Thank you for any help!
Thanks to the wonderful help from Kevin Thorne (Palantir) I have been shown a simple way to handle this type of byte output from the SDK. The code below works beautifully and as expected. I hope this ends up being useful to someone else as well.
SDK READ PNG
from palantir.datasets import dataset
projectPath = '/NIH/Data Management & Analysis Program (DMAP)/nih-dashboards/basic_phenotyper/data/'
filename3 = 'Dashboard Outputs'
datafile = projectPath + filename3
img_result = dataset(datafile) \
.file("dwarf-hotot.png") \
.read().read()
from PIL import Image
import io
image = Image.open(io.BytesIO(img_result))
image.show()
image.save('test.png')
SDK WRITE PNG
in_file = open("test.png", "rb") # opening for [r]eading as [b]inary
pngData= in_file.read() # if you only wanted to read 512 bytes, do .read(512)
in_file.close()
pngFileName = 'Cute-bunny.png'
dataset(datafile) \
.file(pngFileName) \
.write(content = pngData)