Image.load() fails on export with warning "Loading resource as image file, this will not work on export"

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

I’m trying to programmatically load an Image and create an ImageTexture from it. I want to do this so that I can programmatically modify the pixel data of the Image. Here’s some sample code from my TextureRect:

func _ready():
  var image_texture = ImageTexture.new()
  image = Image.new()
  image.load('res://path/to/file.png')
  image.lock()
  image_texture.create_from_image(image, 0)
  self.texture = image_texture

This works fine when I run the game from the editor. However, I get a warning in the debugger that says “Loading resource as image file, this will not work on export.” And thus, when I export to Android, these images are not loaded.

I do not want to use the load() function (as suggested here), because that gives me a StreamTexture and doesn’t allow me to manipulate pixels.

I’ve found a few Github issues about this:

So - is there a good way to make this work for exported games?

:bust_in_silhouette: Reply From: DanteSantana

I had a similar problem, in my case it was when exporting to Windows, have you tried unchecking the option ’Export whit debug’ at the last step of exporting? (below the text box where the name of the .apk file is placed)

You could also try removing the “res://” from the file directory

like this = “res://sour/img.png”
to this = “sour/img.png”

Good luck

Thanks for the help.

I tried removing the res:// from the file path. Interestingly, it got rid of the debugger warning, but the images still don’t show up in the exported version of the game.

I haven’t tried exporting a “release” version of the APK yet, but if that’s necessary, it seems like there’s a bug. I think there should be a better way?

arcticmatt | 2019-08-12 04:13

:bust_in_silhouette: Reply From: Gary_CHAN

I have a similar situation where I asked in here:
https://forum.godotengine.org/49332/how-to-make-a-new-image-with-small-file-size

The related part is the ans by Zylann.

So, I am not sure if it is working for you as I am just a beginner.
But I think if you import you image as an Image (not Texture) you could do something like this:

var image = load("res://path/to/file.png")  # this should give you the Image
image.lock()
{do the pixel things}
image.unlock()
var image_texture = ImageTexture.new()
image_texture.create_from_image(image)
self.texture = image_texture

By not importing as Texture( but Image), GD.load() will not give StreamTexture. Instead, it gives an Image. That’s what I think. Not so sure if I understand all things correctly.

Thanks Gary! This isn’t completely correct (well, at least not for Godot Engine v3.1.1), but it helped me debug :slight_smile: I’ll update my post with what worked for me.

arcticmatt | 2019-08-14 03:31

:bust_in_silhouette: Reply From: arcticmatt

Ok, this is actually pretty simple. The basic idea is that instead of using Image.load to load an image, you can use load() and then call get_data().

var stream_texture = load('res://path/to/file.png')
var image_texture = ImageTexture.new()
image = stream_texture.get_data()
image.lock() # so i can modify pixel data
image_texture.create_from_image(image, 0)

Thanks, this is working.

GameDevJana | 2022-04-07 00:23