How to call save_png_to_buffer()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By namathos

I’m trying to use the save_png_to_buffer() call but I can’t figure out how to use it, I keep getting nonexistent function.

There’s nothing about it in the manual, what is the correct way to call this and how do I get info from the buffer once I have saved it?

PS. If anyone has any resources on understanding how Godot’s buffer works that would also be super helpful!


:bust_in_silhouette: Reply From: rustyStriker

Assuming you are trying to make a screenshot…
use this code as what you found is outdated

var image = get_viewport().get_texture().get_data()

Actually, I do want to save png to buffer, I’m trying to capture the screen and send it to a server via base64 encoding. My app is going to be online so I can’t really store the file locally to process it.

namathos | 2020-06-22 15:22

well in that case you can simply remove the last line and replace it with a function to send the image

rustyStriker | 2020-06-22 15:25

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))
    # Save to path works to test locally but not on server

    # This code does the conversion
    var file = # file to store base64 data
    var image = # file to open saved drawing"res://temp-img.png", File.READ)"res://temp-file.txt", File.READ_WRITE)
    # convert to base64 and store in temp file
    # save base64 data
    data = str(file.get_as_text())

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?

namathos | 2020-06-22 15:48

:bust_in_silhouette: Reply From: namathos

Solved it. Firstly make sure using latest release candidate (3.2.2 in my case), then the following format:


If anyone’s trying to Marshall (like I am), this can be added on:


I assume once 3.2.2 is out on stable release the wiki will be updated at some point.