Create 3d nft art
Generate 3d wall art
!pip3 install numpy-stl
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
from stl import mesh
# Define the 8 vertices of the cube
vertices = np.array([\
[-1, -1, -1],
[+1, -1, -1],
[+1, +1, -1],
[-1, +1, -1],
[-1, -1, +1],
[+1, -1, +1],
[+1, +1, +1],
[-1, +1, +1]])
# Define the 12 triangles composing the cube
faces = np.array([\
[0,3,1],
[1,3,2],
[0,4,7],
[0,7,3],
[4,5,6],
[4,6,7],
[5,1,2],
[5,2,6],
[2,3,6],
[3,7,6],
[0,1,5],
[0,5,4]])
# Create the mesh
cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
print(vertices[f[j],:])
cube.vectors[i][j] = vertices[f[j]]
# Write the mesh to file "cube.stl"
cube.save('cube.stl')
from PIL import Image
import matplotlib.pyplot as plt
im = Image.open("data/images/ctl.jpg")
plt.imshow(im)
grey_img = Image.open('data/images/ctl.jpg').convert('L')
plt.imshow(grey_img)
import numpy as np
from stl import mesh
# Define the 8 vertices of the cube
vertices = np.array([\
[-1, -1, -1],
[+1, -1, -1],
[+1, +1, -1],
[-1, +1, -1]])
# Define the 12 triangles composing the cube
faces = np.array([\
[1,2,3],
[3,1,0]
])
# Create the mesh
cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
cube.vectors[i][j] = vertices[f[j],:]
# Write the mesh to file "cube.stl"
cube.save('surface.stl')
grey_img = Image.open('data/images/ctl.jpg').convert('L')
max_size=(500,500)
max_height=10
min_height=0
#height=0 for minPix
#height=maxHeight for maxPIx
grey_img.thumbnail(max_size)
imageNp = np.array(grey_img)
maxPix=imageNp.max()
minPix=imageNp.min()
print(imageNp)
(ncols,nrows)=grey_img.size
vertices=np.zeros((nrows,ncols,3))
for x in range(0, ncols):
for y in range(0, nrows):
pixelIntensity = imageNp[y][x]
z = (pixelIntensity * max_height) / maxPix
#print(imageNp[y][x])
vertices[y][x]=(x, y, z)
faces=[]
for x in range(0, ncols - 1):
for y in range(0, nrows - 1):
# create face 1
vertice1 = vertices[y][x]
vertice2 = vertices[y+1][x]
vertice3 = vertices[y+1][x+1]
face1 = np.array([vertice1,vertice2,vertice3])
# create face 2
vertice1 = vertices[y][x]
vertice2 = vertices[y][x+1]
vertice3 = vertices[y+1][x+1]
face2 = np.array([vertice1,vertice2,vertice3])
faces.append(face1)
faces.append(face2)
print(f"number of faces: {len(faces)}")
facesNp = np.array(faces)
# Create the mesh
surface = mesh.Mesh(np.zeros(facesNp.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
surface.vectors[i][j] = facesNp[i][j]
# Write the mesh to file "cube.stl"
surface.save('surface.stl')
print(surface)
a = np.zeros((3, 3))
a[:,0]=3
print(a[:,0])
print(a)
! pip install jupyter-cadquery==2.2.1 matplotlib
import cadquery as cq
import cadquery.freecad_impl as cadfc
import matplotlib.pyplot as plt
! pip install vpython
from vpython import *
sphere()
from vpython import *
scene = canvas() # This is needed in Jupyter notebook and lab to make programs easily rerunnable
b = box(pos=vec(-4,2,0), color=color.red)
c1 = cylinder(pos=b.pos, radius=0.1, axis=vec(0,1.5,0), color=color.yellow)
s = sphere(pos=vec(4,-4,0), radius=0.5, color=color.green)
c2 = cylinder(pos=s.pos, radius=0.1, axis=vec(0,1.5,0), color=color.yellow)
t1 = text(text='box', pos=c1.pos+c1.axis, align='center', height=0.5,
color=color.yellow, billboard=True, emissive=True)
t2 = text(text='sphere', pos=c2.pos+c2.axis, align='center', height=0.5,
color=color.yellow, billboard=True, emissive=True)
t3 = text(text='Faces forward', pos=vec(-4,0,0),
color=color.cyan, billboard=True, emissive=True)
box(pos=t3.start, size=0.1*vec(1,1,1), color=color.red)
t4 = text(text='Regular text', pos=vec(-4,-1,0), depth=0.5, color=color.yellow,
start_face_color=color.red, end_face_color=color.green)
box(pos=t4.start, size=0.1*vec(1,1,1), color=color.red)
scene.caption = """<b>3D text can be "billboard" text -- always facing you.</b>
Note that the "Regular text" has different colors on the front, back and sides.
Right button drag or Ctrl-drag to rotate "camera" to view scene.
To zoom, drag with middle button or Alt/Option depressed, or use scroll wheel.
On a two-button mouse, middle is left + right.
Touch screen: pinch/extend to zoom, swipe or two-finger rotate."""
psutil.sensors_battery()