Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import numpy as np
- import trimesh
- # --- Step 1: Load and prepare image ---
- img_path = "ghostcore_image.bmp"
- img = Image.open(img_path).convert("L") # Convert to grayscale
- img = img.resize((256, 256)) # Reduce resolution for mesh simplicity
- # --- Step 2: Create height map ---
- height_map = np.array(img) / 255.0 # Normalize 0.0–1.0
- height_scale = 8.0 # Z-height exaggeration factor
- # --- Step 3: Create 3D mesh from height map ---
- rows, cols = height_map.shape
- vertices = []
- faces = []
- for y in range(rows):
- for x in range(cols):
- z = height_map[y, x] * height_scale
- vertices.append([x, -y, z]) # Flip Y to make upright
- for y in range(rows - 1):
- for x in range(cols - 1):
- i = y * cols + x
- faces.append([i, i + 1, i + cols])
- faces.append([i + 1, i + cols + 1, i + cols])
- vertices = np.array(vertices)
- faces = np.array(faces)
- mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
- # --- Step 4: Export STL ---
- output_path = "ghostcore_wraith_relief.stl"
- mesh.export(output_path)
- print(f"✅ STL exported: {output_path}")
- import torch
- import urllib.request
- import cv2
- import numpy as np
- import os
- from PIL import Image
- import matplotlib.pyplot as plt
- import trimesh
- # 1. Load MiDaS model from torch.hub
- midas = torch.hub.load("intel-isl/MiDaS", "DPT_Large")
- midas.eval()
- midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms").dpt_transform
- # 2. Load your image
- INPUT_IMAGE_PATH = "input.png" # Replace this with your image
- img = cv2.imread(INPUT_IMAGE_PATH)
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
- # 3. Apply MiDaS depth prediction
- input_tensor = midas_transforms(img_rgb).unsqueeze(0)
- with torch.no_grad():
- prediction = midas(input_tensor)
- depth = prediction.squeeze().cpu().numpy()
- # 4. Normalize and resize depth
- depth = cv2.resize(depth, (256, 256))
- depth_normalized = (depth - depth.min()) / (depth.max() - depth.min())
- depth_scaled = depth_normalized * 30 # exaggerate for relief (in mm)
- # 5. Convert depth map into mesh
- rows, cols = depth_scaled.shape
- vertices = []
- faces = []
- for y in range(rows):
- for x in range(cols):
- z = depth_scaled[y, x]
- vertices.append([x, -y, z])
- for y in range(rows - 1):
- for x in range(cols - 1):
- i = y * cols + x
- faces.append([i, i + 1, i + cols])
- faces.append([i + 1, i + cols + 1, i + cols])
- vertices = np.array(vertices)
- faces = np.array(faces)
- mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
- # 6. Export as STL
- OUTPUT_STL_PATH = "generated_model.stl"
- mesh.export(OUTPUT_STL_PATH)
- print(f"✅ STL saved to: {OUTPUT_STL_PATH}")
Advertisement
Add Comment
Please, Sign In to add comment