Rahatara commited on
Commit
08602a0
·
verified ·
1 Parent(s): 728be1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -1,30 +1,40 @@
1
  import gradio as gr
2
  import trimesh
 
3
  import base64
4
  import io
5
 
6
  def modify_texture():
7
  # Load the GLB file
8
- mesh = trimesh.load("train.glb")
9
-
10
- # Load the default texture image
11
- with open("defect.jpg", "rb") as f:
12
- default_texture_data = f.read()
13
-
14
- # Set the texture to the mesh
15
- mesh.visual.material.texture.image = default_texture_data
16
-
17
- # Export the modified mesh to GLB format
18
- modified_mesh_data = mesh.export(file_type="glb")
19
-
20
- # Convert the modified mesh data to BytesIO object
21
- output_file = io.BytesIO(modified_mesh_data)
22
-
23
- return output_file
 
 
 
 
 
 
 
 
 
24
 
25
  gr.Interface(
26
  fn=modify_texture,
27
  inputs=[],
28
- outputs=gr.File(label="Download Modified GLB File"),
29
  title="GLB Texture Modifier"
30
  ).launch()
 
1
  import gradio as gr
2
  import trimesh
3
+ from PIL import Image
4
  import base64
5
  import io
6
 
7
  def modify_texture():
8
  # Load the GLB file
9
+ mesh = trimesh.load_mesh("train.glb", process=False)
10
+
11
+ # Open the texture image
12
+ im = Image.open("defect.jpg")
13
+
14
+ # Create a material with the texture image
15
+ material = trimesh.visual.texture.SimpleMaterial(image=im)
16
+
17
+ # Get the uv coordinates from the mesh
18
+ uv = mesh.visual.uv
19
+
20
+ # Create TextureVisuals with the uv coordinates and the texture image
21
+ color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
22
+
23
+ # Apply the visuals to the mesh
24
+ mesh.visual = color_visuals
25
+
26
+ # Export the mesh to a .glb file
27
+ mesh.export(file_obj='modified_train.glb')
28
+
29
+ # Convert the GLB file to base64 for Gradio Model3D output
30
+ with open('modified_train.glb', 'rb') as f:
31
+ glb_base64 = base64.b64encode(f.read()).decode()
32
+
33
+ return glb_base64
34
 
35
  gr.Interface(
36
  fn=modify_texture,
37
  inputs=[],
38
+ outputs=gr.Model3D(label="Download Modified GLB File"),
39
  title="GLB Texture Modifier"
40
  ).launch()