xosski

stl files fun

Dec 22nd, 2025
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. from PIL import Image
  2. import numpy as np
  3. import trimesh
  4.  
  5. # --- Step 1: Load and prepare image ---
  6. img_path = "ghostcore_image.bmp"
  7. img = Image.open(img_path).convert("L") # Convert to grayscale
  8. img = img.resize((256, 256)) # Reduce resolution for mesh simplicity
  9.  
  10. # --- Step 2: Create height map ---
  11. height_map = np.array(img) / 255.0 # Normalize 0.0–1.0
  12. height_scale = 8.0 # Z-height exaggeration factor
  13.  
  14. # --- Step 3: Create 3D mesh from height map ---
  15. rows, cols = height_map.shape
  16. vertices = []
  17. faces = []
  18.  
  19. for y in range(rows):
  20. for x in range(cols):
  21. z = height_map[y, x] * height_scale
  22. vertices.append([x, -y, z]) # Flip Y to make upright
  23.  
  24. for y in range(rows - 1):
  25. for x in range(cols - 1):
  26. i = y * cols + x
  27. faces.append([i, i + 1, i + cols])
  28. faces.append([i + 1, i + cols + 1, i + cols])
  29.  
  30. vertices = np.array(vertices)
  31. faces = np.array(faces)
  32. mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
  33.  
  34. # --- Step 4: Export STL ---
  35. output_path = "ghostcore_wraith_relief.stl"
  36. mesh.export(output_path)
  37. print(f"✅ STL exported: {output_path}")
  38.  
  39. import torch
  40. import urllib.request
  41. import cv2
  42. import numpy as np
  43. import os
  44. from PIL import Image
  45. import matplotlib.pyplot as plt
  46. import trimesh
  47.  
  48. # 1. Load MiDaS model from torch.hub
  49. midas = torch.hub.load("intel-isl/MiDaS", "DPT_Large")
  50. midas.eval()
  51. midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms").dpt_transform
  52.  
  53. # 2. Load your image
  54. INPUT_IMAGE_PATH = "input.png" # Replace this with your image
  55. img = cv2.imread(INPUT_IMAGE_PATH)
  56. img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  57.  
  58. # 3. Apply MiDaS depth prediction
  59. input_tensor = midas_transforms(img_rgb).unsqueeze(0)
  60. with torch.no_grad():
  61. prediction = midas(input_tensor)
  62. depth = prediction.squeeze().cpu().numpy()
  63.  
  64. # 4. Normalize and resize depth
  65. depth = cv2.resize(depth, (256, 256))
  66. depth_normalized = (depth - depth.min()) / (depth.max() - depth.min())
  67. depth_scaled = depth_normalized * 30 # exaggerate for relief (in mm)
  68.  
  69. # 5. Convert depth map into mesh
  70. rows, cols = depth_scaled.shape
  71. vertices = []
  72. faces = []
  73.  
  74. for y in range(rows):
  75. for x in range(cols):
  76. z = depth_scaled[y, x]
  77. vertices.append([x, -y, z])
  78.  
  79. for y in range(rows - 1):
  80. for x in range(cols - 1):
  81. i = y * cols + x
  82. faces.append([i, i + 1, i + cols])
  83. faces.append([i + 1, i + cols + 1, i + cols])
  84.  
  85. vertices = np.array(vertices)
  86. faces = np.array(faces)
  87.  
  88. mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
  89.  
  90. # 6. Export as STL
  91. OUTPUT_STL_PATH = "generated_model.stl"
  92. mesh.export(OUTPUT_STL_PATH)
  93.  
  94. print(f"✅ STL saved to: {OUTPUT_STL_PATH}")
  95.  
Tags: stl
Advertisement
Add Comment
Please, Sign In to add comment