The screen shot in the save game. (porting a project to Godot 3.0)

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

In my project provided by the save game. In addition to the game data, I save a screenshot (previews). The screenshot I save is as follows:

yield(get_tree(),"idle_frame")
yield(get_tree(),"idle_frame")
Image=get_viewport().get_texture().get_data()
Image.flip_y()
Image.resize(1280,720)
Image.save_png("user://1.png")

Then I upload this image for viewing:

TextureRect.set_texture(load("user://1.png"))

And then I have 2 problems:

  1. After application of the method: Image.resize(1280,720) I get a completely black image of the specified size. Without this method, it is stored correctly.

  2. TextureRect.set_texture(load("user://1.png")) - it worked in Godor 2, but it doesn’t work in Godot 3.

FYI, if you use Image as variable name, it will overwrite the actual Image class, preventing you from using it ever again… due to this bug: GDScript classes can be replaced by anything · Issue #8791 · godotengine/godot · GitHub

Zylann | 2018-02-16 22:36

No, I don’t use Image as the variable name. This also applies to TextureRect. I changed their names so as not to explain what class they were.

DimitriyPS | 2018-02-19 13:44

:bust_in_silhouette: Reply From: Zylann

I believe you cannot use load on a PNG, because load is expecting an imported asset from within your project.
You can load an external image with this code:

var im = Image.new()
im.load("user://your/image.png")
var tex = ImageTexture.new()
tex.create_from_image(im)

Resizing seems to work fine for me on a loaded image… but it behaves strangely in the screenshot demo. Maybe resizing images coming from a screenshot specifically has a bug?

I opened an issue Resizing an Image captured from Viewport results in empty image · Issue #16764 · godotengine/godot · GitHub

var im = Image.new()
im.load("user://your/image.png")
var tex = ImageTexture.new()
tex.create_from_image(im)

Next,how to pass this texture TextureRect?

DimitriyPS | 2018-02-19 13:48

get_node("YourTextureRect).texture = tex

Zylann | 2018-02-19 22:00

TextureRect.set_texture(tex)

It also works.

DimitriyPS | 2018-02-20 07:46

It works on a variable too, but… again, did you named this other variable TextureRect? This is the name of an existing class, please take a look at this bug: GDScript classes can be replaced by anything · Issue #8791 · godotengine/godot · GitHub

Zylann | 2018-02-20 21:47

Yeah, I know. I have already answered under the first post.

No, I don’t use Image as the variable name. This also applies to
TextureRect. I changed their names so as not to explain what class
they were.

DimitriyPS | 2018-02-21 07:02