#Base64 to Texture2D

21 messages · Page 1 of 1 (latest)

dapper socket
#

Hey! I'm trying to link my StableDiffusion client which generates images via an endpoint to godot 4, currently I am successfully fetching the image's base64 and it's all well and good, I tested the base64 and it's valid through multiple services and different languages such as JS and Java, my question is:

How would I be able to turn a Base64 into a Texture? Right now the following is my code:

    if foundImages["image"] != null:
        var bytes = Marshalls.base64_to_raw(foundImages["image"])

        print("Got " + str(bytes.size()) + " bytes")

        var i = Image.new()
        i.load_png_from_buffer(bytes)
        var t = ImageTexture.new()
        t.create_from_image(i);
        
        $CanvasLayer/TextureRect.texture = i; # This is a temporary debug UI Element for me to see if the texture shows up, but all it does it looks like the attached screenshot ( nothing on screen )
        
        print("Created image texture, trying to instanciate and spawn");

        # Spawn a polaroid
        var polaroid: RigidBody3D = polaroidImageScene.instantiate();
        get_tree().root.add_child(polaroid)
        polaroid.global_transform = cardSpawnPoint.global_transform;

        # Try and find the child
        var polaroidModel: PolaroidModel = polaroid.find_child("PolaroidModel");
        polaroidModel.setPolaroidImage(t); # This is a function from the polaroidModel, the function does run
dapper socket
#

After working on this some more, I really think it's a bug in Godot. I wrote the bytes to file and it works just fine

#
var outimg = FileAccess.open("C:/Users/kato/Documents/q/outfile.png", FileAccess.WRITE)
outimg.store_buffer(bytes);
outimg.close();
#

This works perfectly fine

dapper socket
#

Update:
Can not load ANY images, not only base64 ( From scripting )

chrome crag
#

Note, create_from_image() is static method.

dapper socket
#

wait

#

Are you joking rn?

#

oh my god man

#

you literally fixed it for me

#

What the hell, all online tutorials and examples use it as a function on the instance

#

damn, thank you so much.

#

Didn't know this was changed from 3.x -> 4

chrome crag
#

so code should be:

var texture = ImageTexutre.create_from_image(...)

load_png_from_buffer

#

oops 😄

dapper socket
#

Yeah! It's all working perfectly fine now.

chrome crag
dapper socket
#

Thank you so much

#

I'll pay more attention to the docs as there are many small changes.

chrome crag
#

Most tutorials you find online are for 3.5

dapper socket
#

Thanks again for taking the time