Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. from scipy.spatial import Delaunay
  2. import numpy as np
  3. import random
  4. import PIL
  5. import PIL.Image as Image
  6. from PIL import ImageTk
  7. from graphics import *
  8. import time
  9.  
  10. """
  11.  
  12. How does it work?
  13.  
  14. - First, the program plots 1000 points randomly on a grid.
  15.  
  16. - Second, using the Delaunay Triangulation, the program triangulates every point.
  17. - It creates an object (named tri) that acts as a grid
  18. - In this, you can iterate through all of the triangles (simplices)
  19. and get the color of the image at the center of that triangle.
  20. - To break it down:
  21. > Iterate through every triangle in the grid
  22. > Get the center of that triangle
  23. > Find the color of the pixel on the image at the center of the triangle
  24.  
  25. - Lastly, it iterates through every triangle and graphs the vertices of
  26. that triangle to the grid, with the color at the circumcenter.
  27. """
  28.  
  29. lastimg = [5, "P-2.png"] # Used so the same image isnt used twice. 5 is a placeholder.
  30.  
  31. while True:# The main loop, this will run forever until stopped manually.
  32. win = GraphWin("Delaunay Triangulation - Calvin Davis", 1200,1000) # The grid
  33. choices = ["I-" + str(x) + ".png" for x in range(0, 20)] # The pictures
  34. img = random.choice(choices)
  35. img = "I-9.gif"
  36.  
  37. if img in lastimg:
  38. continue
  39. lastimg[0] = img
  40.  
  41. pixelsg = Image(Point(0,0), img)
  42. copy = pixelsg
  43. width, height = copy.getWidth(), copy.getHeight() # Gets the width and height of the image
  44. pixelsg = Image(Point(width/2, height/2), img)
  45. #pixelsg.draw(win)
  46. time.sleep(1)
  47.  
  48.  
  49. p = np.array([[0, 0], [0, height], [width, 0], [width, height]])# The list in which the points go
  50. blacklist = []
  51. for i in range(500): # Randomize the points on a plane
  52. x = random.choice(range(0, width, 5))
  53. y = random.choice(range(0, height, 5))
  54. if [x, y] not in blacklist: # Dont repeat any points
  55. p = np.vstack([p, [x, y]])
  56. blacklist.append([x, y])
  57. tri = Delaunay(p)
  58.  
  59. simplexVIs = [] # "Simplex Vertex indices"
  60. for simplex in tri.vertices:
  61. simplexVIs.append(simplex) # Not important
  62.  
  63. simplexVs = [] # "Simplex Vertices"
  64. for vertexTriplet in simplexVIs: # Grab the vertices of every simplex and add to a list
  65. templist = []
  66. for vertex in vertexTriplet:
  67. templist.append(tri.points[vertex])
  68. simplexVs.append(templist) # Get the 3 vertices that make the triangle
  69.  
  70.  
  71. for simplex in simplexVs:# iterate through every triangle
  72. poly = []
  73. for vertex in simplex: # Keep in mind vertex is an X, Y pair
  74. poly.append([vertex[0], vertex[1]])
  75. # Now we have the polygon
  76. ax = (poly[0][0] + poly[1][0] + poly[2][0]) * (1.0/3)
  77. ay = (poly[0][1] + poly[1][1] + poly[2][1]) * (1.0/3) # The center of the triangle
  78. rgb = pixelsg.getPixel(int(ax), int(ay)) # The color at the center of the triangle
  79. for item in range(len(poly)):# Turning the vertices into points that the graph can understand
  80. poly[item] = Point(poly[item][0], poly[item][1])
  81. triangle = Polygon(poly) # Plotting the triangle
  82. triangle.setFill(color_rgb(rgb[0], rgb[1], rgb[2]))
  83. triangle.setOutline(color_rgb(rgb[0], rgb[1], rgb[2]))
  84. triangle.draw(win)
  85.  
  86. """
  87. plt.triplot(p[:,0], p[:,1], tri.vertices)
  88. plt.plot(p[:,0], p[:,1], "o")
  89. plt.show()
  90. """
  91. print "Done!"
  92. time.sleep(2)
  93. win.close()
  94. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement