Advertisement
Guest User

Untitled

a guest
May 2nd, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import matplotlib.image as mpimg
  3. import scipy.signal as signal
  4. import numpy as np
  5.  
  6. # Tweakable
  7. strip_count = 12 # amount of subdivisions on x-axis
  8. effect_power = 12 # bump effect strength
  9. hMap = mpimg.imread("h3.png") # heightmap to apply
  10. cMap = 'viridis' # matplotlib colormap
  11.  
  12. imgRes = [int(len(hMap[0])),int(len(hMap))]
  13. stripSize = [int(imgRes[0]/strip_count),imgRes[1]]
  14. final = np.zeros((imgRes[1],imgRes[0])) # placeholder for final image, filled with .0
  15.  
  16. def draw_strip(id): # -> void
  17.         for y in range(len(noise)):
  18.             for x in range(len(noise[0])):
  19.                 # calculate pixel movement based on heightmap
  20.                 hv = int(hMap[y][x+stripSize[0]*id][0]*effect_power)
  21.                 final[y][x+stripSize[0]*id] = final[y][x-stripSize[0]+stripSize[0]*id+hv]
  22.  
  23. def get_smooth_noise(size):
  24.     n = np.random.uniform(1.,.0,size)
  25.     n = signal.convolve2d(n, np.ones((4,4)) / 16,"same")
  26.     for y in range(len(n)):
  27.         for i in range(0,2):
  28.             n[y][i] = (n[y][i+1] + n[y][i+2] + n[y][i+3] + n[y][i+4])/4
  29.         n[y][len(n[0])-1] = (n[y][0] + n[y][1] + n[y][2])/3
  30.     return n
  31.  
  32. def create_stereogram():
  33.     # Drawing first reference strip of noise
  34.     for y in range(len(noise)):
  35.         for x in range(len(noise[0])):
  36.             final[y][x] = noise[y][x]
  37.  
  38.     for i in range(1,strip_count):
  39.         draw_strip(i)
  40.  
  41.     pltimg = plt.imshow(final,cmap=cMap)
  42.     plt.show()
  43.  
  44. noise = get_smooth_noise((stripSize[1],stripSize[0]))
  45. create_stereogram()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement