Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pygltflib import GLTF2, Texture, Image
|
3 |
+
import base64
|
4 |
+
import io
|
5 |
+
|
6 |
+
def modify_texture(glb_file, new_texture):
|
7 |
+
# Load the GLB file
|
8 |
+
gltf = GLTF2().load(glb_file.name)
|
9 |
+
|
10 |
+
# Convert the new texture image to use in GLB
|
11 |
+
img_byte_arr = io.BytesIO()
|
12 |
+
new_texture.save(img_byte_arr, format='PNG')
|
13 |
+
encoded_img = base64.b64encode(img_byte_arr.getvalue()).decode('ascii')
|
14 |
+
|
15 |
+
# Assuming there's at least one image in the original GLB
|
16 |
+
if gltf.images:
|
17 |
+
# Replace the first image with the new texture
|
18 |
+
gltf.images[0].uri = f"data:image/png;base64,{encoded_img}"
|
19 |
+
else:
|
20 |
+
# Add new image if none exists
|
21 |
+
new_image = Image(uri=f"data:image/png;base64,{encoded_img}")
|
22 |
+
gltf.images.append(new_image)
|
23 |
+
# Update texture to point to the new image
|
24 |
+
new_texture = Texture(source=len(gltf.images)-1)
|
25 |
+
gltf.textures.append(new_texture)
|
26 |
+
|
27 |
+
# Save the modified GLB to a temporary file and return it
|
28 |
+
output_file = io.BytesIO()
|
29 |
+
gltf.save(output_file)
|
30 |
+
output_file.seek(0)
|
31 |
+
return output_file
|
32 |
+
|
33 |
+
with gr.Blocks() as app:
|
34 |
+
with gr.Row():
|
35 |
+
with gr.Column():
|
36 |
+
glb_input = gr.File(label="Upload GLB File")
|
37 |
+
texture_input = gr.Image(label="Upload New Texture Image", tool="editor")
|
38 |
+
with gr.Column():
|
39 |
+
modified_glb_output = gr.File(label="Download Modified GLB File")
|
40 |
+
|
41 |
+
glb_input.change(modify_texture, inputs=[glb_input, texture_input], outputs=[modified_glb_output])
|
42 |
+
|
43 |
+
app.launch()
|