Can I save a .png in a Foundry Code Workbook?

415 views Asked by At

I'm using a Foundry Code Workbook to generate an image and then am trying to save it back into Foundry. I can generate the image fine but am struggling with saving it.

Could you please help me understand how to save a .png image file via a Code Workbook?

2

There are 2 answers

0
Andrew St P On

Code Workbook uses the Agg backend and saves the figure as .png, which you can use for reference in your attempt to save the .png bytes to Foundry. Specifying the backend and specifying the savefig output format will require a backend configuration change, which can be made by the Palantir Support Team.

0
Rasmus M On

This is possible using raw file access in python. Assuming you've selected a Python Transform Input as the input type, the following code (for matplotlib) will save a PNG to a dataset.

import matplotlib.pyplot as plt
plt.scatter([1, 2, 3], [1, 2, 3])
plt.show()

output = Transforms.get_output()
output_fs = output.filesystem()
plt.savefig('test.png')
img = open('test.png', 'rb').read()

with output_fs.open('png_test.png', 'wb') as f:
    f.write(img)
    f.close()