I should bring in some context. Firstly, I'm doing this on the newest release candidate 3.2.2 since that does have save_png_to_buffer()
. I'm making a drawing app, on saving the drawing gets sent to a server. The server has a REST API it's decoding base64 to png. The app is all going to be online so I can't save the drawing locally to be processed, which is where the problem is. To Marshall the png I need to have it present, I've been successful in doing it without the buffer but I need to process it in memory because the app will be online.
#This fetches the drawing
func save_picture():
# Get the viewport image.
var img = get_viewport().get_texture().get_data()
# Crop the image so we only have canvas area.
var cropped_image = img.get_rect(Rect2(TL_node.global_position, IMAGE_SIZE))
cropped_image.flip_y()
# Save to path works to test locally but not on server
cropped_image.save_png("res://temp-img.png")
# This code does the conversion
var file = File.new() # file to store base64 data
var image = File.new() # file to open saved drawing
image.open("res://temp-img.png", File.READ)
file.open("res://temp-file.txt", File.READ_WRITE)
# convert to base64 and store in temp file
file.store_string(Marshalls.raw_to_base64(image.get_buffer(image.get_len())))
image.close()
# save base64 data
data = str(file.get_as_text())
file.close()
The only way I've been able to do this is by having two files locally, one png one txt file for doing the conversion. Haven't been successful any other way so far. Unless there's a way for me to do this processing online?