[Godot 3] Capturing Viewport + resizing it + saving it to png

Hello everyone!

This past week I've been working on porting my project from Godot 2 to Godot 3 and I'm almost finished, but there's this small issue I can't seem to solve.

In my game, you can take screenshots by pressing a key. However, the game has a tiny resolution, so I'd like to be able to resize the screenshots before they are saved to the disk. In Godot 2 I could achieve this easily by using get_viewport().queue_screen_capture() and capture.resized(width, height, interpolation), but for some reason I can't get resized() (now resize()) to work properly in Godot 3. This is the code:

# get data
var img = get_viewport().get_texture().get_data()

# wait two frames
yield(get_tree(), "idle_frame")
yield(get_tree(), "idle_frame")

# flip

# get screen ratio + resize capture
var ratio = load_config("screenshot_ratio")
if ratio == null: ratio = 2


# save to file

I get no error output, and the picture saves alright if I don't resize it, but if I do, all I get is a blank image the same color as the viewport background.

I guess I'm missing something? I'd really appreciate any help!


Tags :


  • TwistedTwiglegTwistedTwigleg Posts: 982Admin
    Answer ✓

    It seems resize is not working right now (at least, nothing seems to be working in my tests). A Github issue with an example project should probably be opened at some point, as neither of the resize functions (resize and resize_to_po2) working is almost certainly a bug.

    For now you can use expand_x2_hq2x to scale the images to be twice as large. Also, you need to put your yield(get_tree(), "idle_frame" calls above img = get_viewport().get_texture().get_data() :wink:

  • AbsurdinessAbsurdiness Posts: 3Member

    Thanks for the fast response! :) I appreciate it.

    So it's a bug, then? It was really giving me a headache. I may open the issue myself when I find the time.

    Haha, thank you. I'm a beginner programmer, so I don't know what I'm doing sometimes.

  • TwistedTwiglegTwistedTwigleg Posts: 982Admin

    Yeah, I think it's a bug. If you want to open a Github issue, that'd be great!

    As for being a beginner, everyone has to start somewhere! (And if I'm being honest, I'm not sure if I'm a beginner or not in programming. I've just been doing it for awhile :sweat_smile: )

Sign In or Register to comment.