0 votes

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!

Thanks!

in Engine by (39 points)
retagged by

2 Answers

0 votes
Best answer

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

image.save_png_to_buffer()

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

Marshalls.raw_to_base64(image.save_png_to_buffer())

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

by (39 points)
selected by
0 votes

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()
    image.flip_y()
    image.save_png("/path/to/file.png")
by (1,190 points)

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.

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

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.