edwardthefma commited on
Commit
55f3eec
·
verified ·
1 Parent(s): 197eb9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -18,26 +18,38 @@ def generate_texture(stl_file):
18
  uv /= uv.max(axis=0) + 1e-10 # Avoid division by zero
19
  mesh.visual.uv = uv
20
 
21
- # Generate a simple texture (e.g., gradient based on UV coordinates)
22
  texture_size = (512, 512) # Texture resolution
23
- texture = np.zeros((texture_size[0], texture_size[1], 3), dtype=np.uint8)
24
 
25
- # Create a gradient texture (R, G based on UV coordinates)
26
- for i in range(texture_size[0]):
27
- for j in range(texture_size[1]):
28
- u, v = i / texture_size[0], j / texture_size[1]
29
- texture[i, j] = [int(u * 255), int(v * 255), 128] # RGB gradient
30
 
31
- # Alternatively, generate a normal map (optional)
32
- # normals = mesh.vertex_normals
33
- # texture = ((normals + 1) * 127.5).astype(np.uint8) # Convert normals to RGB
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # Save texture as PNG
36
  texture_img = Image.fromarray(texture)
37
  output_path = "texture_output.png"
38
  texture_img.save(output_path)
39
 
40
- return output_path, "Texture generated successfully!"
41
  except Exception as e:
42
  return None, f"Error: {str(e)}"
43
 
@@ -49,8 +61,8 @@ iface = gr.Interface(
49
  gr.File(label="Download Texture (PNG)"),
50
  gr.Textbox(label="Status")
51
  ],
52
- title="STL Texture Generator",
53
- description="Upload an STL file to generate a texture and export it as a PNG."
54
  )
55
 
56
  # Launch the app
 
18
  uv /= uv.max(axis=0) + 1e-10 # Avoid division by zero
19
  mesh.visual.uv = uv
20
 
21
+ # Create a texture image
22
  texture_size = (512, 512) # Texture resolution
23
+ texture = np.zeros((texture_size[0], texture_size[1], 3), dtype=np.uint8) # Black background
24
 
25
+ # Map UV coordinates to texture pixels and highlight in red
26
+ uv = mesh.visual.uv
27
+ pixel_coords = (uv * np.array(texture_size)).astype(int) # Map UVs to pixel space
28
+ pixel_coords = np.clip(pixel_coords, 0, np.array(texture_size) - 1) # Ensure within bounds
 
29
 
30
+ # Highlight pixels corresponding to UV coordinates in red
31
+ for u, v in pixel_coords:
32
+ texture[v, u] = [255, 0, 0] # Red for model areas
33
+
34
+ # Optional: Fill faces by rasterizing triangles (more accurate coverage)
35
+ if hasattr(mesh, 'faces'):
36
+ for face in mesh.faces:
37
+ # Get UVs for the face's vertices
38
+ face_uvs = uv[face]
39
+ # Convert to pixel coordinates
40
+ face_pixels = (face_uvs * np.array(texture_size)).astype(int)
41
+ face_pixels = np.clip(face_pixels, 0, np.array(texture_size) - 1)
42
+ # Approximate triangle fill by setting a small region around vertices
43
+ for px, py in face_pixels:
44
+ texture[max(0, py-2):min(texture_size[1], py+3),
45
+ max(0, px-2):min(texture_size[0], px+3)] = [255, 0, 0]
46
 
47
  # Save texture as PNG
48
  texture_img = Image.fromarray(texture)
49
  output_path = "texture_output.png"
50
  texture_img.save(output_path)
51
 
52
+ return output_path, "Texture generated with model areas highlighted in red!"
53
  except Exception as e:
54
  return None, f"Error: {str(e)}"
55
 
 
61
  gr.File(label="Download Texture (PNG)"),
62
  gr.Textbox(label="Status")
63
  ],
64
+ title="STL Texture Generator with Highlight",
65
+ description="Upload an STL file to generate a texture with model areas highlighted in red, exported as a PNG."
66
  )
67
 
68
  # Launch the app