Image loading from user://

Godot Version

v4.2 on Ubuntu 23.04

Question

I am trying to load an image from the user directory and show it in a TextureRect called ‘photo’. The image is definately there, there are no errors loading it, but it does not show in the TextureRect:

	print("load image:"+cache_path)
	var image = Image.new()
	var err = image.load(cache_path)
	if err != OK:
		print("File not loaded:",err)
	var texture = ImageTexture.new()
	texture.create_from_image(image)
	$photo.texture = texture

It also does not load from res://, but when I use

var texture = load(cache_path)

it works loading from res:// but still it does not not from user://

Are you sure this file is available in user:// means Godot user app data??

yes, this one is complicated
the way you can load your internal game image with path is by load()
and the resource should be imported as Image, not Texture2D
image
after you load this image, you can just save_jpg or save_png to (“user://saved_image.jpg”)

using FileAccess and DirAccess for loading/using res:// would work fine in editor
but once you exported it, it will say you cant get the res:// image on exported project

Yes… this is the scenario:
When an image is requested, the app checks if it is in user://cache. If not, it is downloaded there first.

I have checked by looking in /home/fedor/.local/share/godot/app_userdata/Nr2b/cache myself, the files are there and can be opened with an image viewer. So no corruption too.

the app also checks at run time, using this code:

if FileAccess.file_exists(cache_path):
		print("getting image from cache:"+cache_path)
		loadImageToTexRect()
		return;

And it does output the confirmation that the image is there in cache (and does not need te be downloaded)

So I am absolutely positive that the image is there :grinning:

It is not entirely clear to me what you mean: I am loading existing and valid JPG’s during runtime in a GD-script. I am not sure what you mean bij importing as Image and why I would need to save an already existing image.

Could you elaborate with some example code?

Found it!

It turns out create_from_image() is a static method of ImageTexture. So I changed the code to this:

	print("load image:"+cache_path)
	
	var image = Image.new()
	var err = image.load(cache_path)
	if err != OK:
		print("File not loaded:",err)
	
	var texture=ImageTexture.create_from_image(image)
	$photo.texture = texture

And now it works!

Note to self: keep checking the debugger messages, even the yellow ones.

try exporting this project in windows, see if it can load the file again from res:// path

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.