Guest User

Untitled

a guest
May 12th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import division
  4. from PIL import Image
  5. from PIL import ImageDraw
  6. from random import randint
  7. from random import uniform
  8. import time
  9. import math
  10. tid_start = float(time.time())
  11.  
  12.  
  13.  
  14.  
  15.  
  16. #
  17. #
  18. # hey man, do your config up here
  19. # for all of these, larger = longer generation time
  20. # i'm @cat_abyss by the way nice to meet you
  21. #
  22. #
  23.  
  24. size = 500 # size of the image, will be a square, so 500x500
  25. n_closest = randint(1,10) # draw lines to the N closest points of the same color
  26. TAU = 2.0 * math.pi # just a constant, whoops
  27. gray_steps = randint(1,6) # how many different shades of gray we want
  28. max_distance = 20 # max distance from a point to search for neighbours
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. #
  36. # OK BEGIN THE SHIT
  37. #
  38.  
  39. canvas = [[0.0 for x in range(size)] for x in range(size)]
  40.  
  41. print "n_closest: " + str(n_closest)
  42. print "gray_steps: " + str(gray_steps)
  43.  
  44. #
  45. # FUNKTIONER
  46. #
  47. def applymap(x, y, master, gray_steps):
  48. mmap = [ [1, 13, 4, 14], [9, 5, 12, 8], [3, 14, 2, 14], [11, 7, 10, 6] ] # dithering map
  49. temp = mmap[x%4][y%4]/16
  50. a = master+(temp*master)
  51. a = round(a*gray_steps)/gray_steps
  52. return a
  53.  
  54. #
  55. # DEL ETT - ARKADER
  56. #
  57. print "GENERERAR ARKADER"
  58. var_a = randint(0,600)-300;
  59. var_b = randint(0,3000)-1500;
  60. var_c = randint(0,600)-300;
  61. var_d = randint(0,3000)-1050;
  62. var_e = round(randint(0,1000)-500);
  63. var_f = round(randint(0,1000)-500);
  64.  
  65. for y in range(0,size):
  66. for x in range(0,size):
  67. x_new = x + var_e;
  68. y_new = y + var_f;
  69.  
  70. if math.tan( (x_new+var_a)/var_b+(y_new/var_a) ) * math.tan( (y_new/(var_a*var_b)) + (x_new*2)) > uniform(0,1):
  71. if math.tan( (y_new/var_d)*x_new ) > 0:
  72. kek = math.tan(y_new/(var_a*var_b)) + (x_new*2)
  73. canvas[y][x] = 1.0-kek
  74. else:
  75. kek = math.cos((y_new/(var_a*var_b))*(x_new/2))
  76. canvas[y][x] = 1.0-kek
  77.  
  78. #
  79. # DEL ETT KOMMA FEM: CLIPPING?
  80. #
  81.  
  82. for y in range(0,size):
  83. for x in range(0,size):
  84. if canvas[y][x] > 1: canvas[y][x] = 1.0
  85. if canvas[y][x] < 0: canvas[y][x] = 0.0
  86.  
  87.  
  88. #
  89. # DEL TVÅ: CIRKEL RUNT ALLT
  90. #
  91. print "GENERERAR RAM"
  92.  
  93.  
  94. for y in range(0,size):
  95. for x in range(0,size):
  96. kek = 1.0 - ((math.cos(x*(TAU/size))+1)/2) # x-axeln
  97. kek = kek*(1.0-((math.cos(y*(TAU/size))+1)/2))
  98. if kek > 1: kek = 1.0
  99. if kek < 0: kek = 0.0
  100. canvas[y][x] = kek * canvas[y][x]
  101.  
  102.  
  103. for y in range(0,size):
  104. for x in range(0,size):
  105. if canvas[y][x] > 1: canvas[y][x] = 1
  106. if canvas[y][x] < 0: canvas[y][x] = 0
  107.  
  108. #
  109. # DEL TRE: LINJER OCH DET
  110. #
  111.  
  112. print "FLYTTAR CANVAS TILL GOLFPUTT"
  113.  
  114. golfputt = Image.new("RGB", (size,size), 1)
  115. for y in range(0,size):
  116. for x in range(0,size):
  117. useme = applymap(x%size,y,canvas[y][x],gray_steps)
  118. colour = 255-int(255*useme)
  119. golfputt.putpixel((x,y), (colour,colour,colour))
  120.  
  121. draw = ImageDraw.Draw(golfputt)
  122.  
  123. print "GENERERAR LINJER ETC"
  124.  
  125. for cykler in range(1,gray_steps+1):
  126. print "> lager " + str(cykler)
  127. passad_cykler = cykler/gray_steps
  128.  
  129. how_many_points = 0
  130. for y in range(0,size):
  131. for x in range(0,size):
  132. useme = applymap(x%size,y,canvas[y][x],gray_steps)
  133. if useme == passad_cykler:
  134. how_many_points = how_many_points + 1
  135.  
  136. print " > DET BLIR " + str(how_many_points) + " PUNKTER"
  137. point = 0
  138. points = [[0 for x in range(3)] for x in range(how_many_points)] # 0 = x, 1 = y, 2 = distance
  139. for y in range(0,size):
  140. for x in range(0,size):
  141. useme = applymap(x%size,y,canvas[y][x],gray_steps)
  142. if useme == passad_cykler:
  143. points[point][0] = x;
  144. points[point][1] = y;
  145. point = point + 1
  146.  
  147. print " > HITTAR PROXIMITY POINTS"
  148. painting_color = int((1.0-passad_cykler)*255)
  149. painting_color = (painting_color,painting_color,painting_color)
  150.  
  151. linje_start = float(time.time())
  152. tid_math = 0
  153. tid_mathx = 0
  154. tid_draw = 0
  155. for master in range(0, how_many_points):
  156. if master%1000 == 0:
  157. print str(round((master/how_many_points)*100)) + "% - " + str(master) + " of " + str(how_many_points)
  158. print " > DISTANCE: " + str(tid_math) + " SORT: " + str(tid_mathx) + " DRAW: " + str(tid_draw)
  159.  
  160. tid_math_2 = float(time.time())
  161. newlist = []
  162. for slave in range(0, how_many_points):
  163. kek_x = abs(points[master][0] - points[slave][0])
  164. kek_y = abs(points[master][1] - points[slave][1])
  165. if kek_x < max_distance and kek_y < max_distance:
  166. points[slave][2] = math.sqrt((kek_x**2)+(kek_y**2))
  167. newlist.append(points[slave])
  168.  
  169. tid_math = tid_math + float(time.time()) - tid_math_2
  170. tid_math_2 = float(time.time())
  171.  
  172. newlist.sort(key=lambda x: x[2])
  173.  
  174. tid_mathx = tid_mathx + float(time.time()) - tid_math_2
  175. tid_math_2 = float(time.time())
  176. for i in range(1,(n_closest+1)%len(newlist)):
  177. draw.line((points[master][0],points[master][1], newlist[i][0],newlist[i][1]), fill = painting_color)
  178. tid_draw = tid_draw + float(time.time()) - tid_math_2
  179.  
  180. #
  181. # DEL SIST: GÖR EN BILD
  182. #
  183.  
  184.  
  185.  
  186.  
  187.  
  188. print "SAVING"
  189. golfputt.save("output.png")
Advertisement
Add Comment
Please, Sign In to add comment