Alexandr
Alexandr

Reputation: 29

Load texture from image by global path in godot

var img = Image.new() 
var texture = ImageTexture.new() 
var file: FileAccess = FileAccess.open(lang_path + "logo.png", FileAccess.READ) 
img.load_png_from_buffer(file.get_buffer(file.get_length())) 
file.close() 
print(lang_path + "logo.png") 
texture.create_from_image(img) 
print(texture.get_size())  

lang_path is global path of needed folder(like E:/langs/gaydot/zero/langs/) and when compilated wont be in exe_name.pck as other files

I need to load image as textures for var icon: TextureRect = TextureRect.new() icon.texture = task.lang.img and texture later should be in task.lang.img BUT its does not work and code above prints something like

E:/langs/gaydot/zero/langs/Python/logo.png

(0, 0)

That means texture have no size

If use usual icon.texture = load(task.lang.img) it will work but only if file is in exe_name.pck

var file: FileAccess = FileAccess.open(lang_path + "logo.png", FileAccess.READ) img.load_png_from_buffer(file.get_buffer(file.get_length())) 

wont work as well as

img.load(lang_path + "logo.png")

or

img.load_from_file(lang_path + "logo.png")

if use just texture = load(lang_path + "logo.png") then it will work while not compiled but when compiled will be error

ERROR: Unable to open file: res://.godot/imported/logo.png-3df1515965a2eab0b2ad2d33c094f816.ctex.
   at: (scene/resources/compressed_texture.cpp:41)
ERROR: Failed loading resource: res://.godot/imported/logo.png-3df1515965a2eab0b2ad2d33c094f816.ctex. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:283)
ERROR: Failed loading resource: C:/Users/user/Desktop/out/langs/Python/logo.png. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:283)

Upvotes: 0

Views: 120

Answers (1)

Alexandr
Alexandr

Reputation: 29

Actually, load_from_file and create_from_image are not procedures but functions and return Image and ImageTexture, so solution was just

var img = Image.new().load_from_file(lang_path + "logo.png")
var texture = ImageTexture.new().create_from_image(img)

Upvotes: 0

Related Questions