import gradio as gr import trimesh import numpy as np from PIL import Image import base64 def text_image_to_glb(text, image): """ Converts text and image into a 3D GLB object. Args: text (str): Input text. image (PIL.Image): Input image. Returns: bytes: GLB file as bytes. str: HTML for 3D preview. """ # Create a simple 3D mesh (e.g., a cube) mesh = trimesh.creation.box(extents=[1, 1, 1]) # Create a 1x1x1 cube # Add text to the 3D object (as metadata) mesh.metadata['text'] = text # Convert the image to a texture and apply it to the mesh if image: image = np.array(image) # Convert PIL image to numpy array texture = trimesh.visual.TextureVisuals(image=image) mesh.visual = texture # Export the mesh to GLB format glb_data = trimesh.exchange.gltf.export_glb(mesh) # Encode the GLB data as base64 for the HTML preview glb_base64 = base64.b64encode(glb_data).decode("utf-8") # Create an HTML preview for the 3D object html_preview = f"""
Text: {text}
Image: Applied as texture