Guest User

Untitled

a guest
Apr 2nd, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import math
  2. import sys
  3. import time
  4. import random
  5.  
  6. import requests
  7. from PIL import Image
  8. from requests.adapters import HTTPAdapter
  9.  
  10. img = Image.open(sys.argv[1])
  11. origin = (int(sys.argv[2]), int(sys.argv[3]))
  12. username = sys.argv[4]
  13. password = sys.argv[5]
  14. percent = 0
  15.  
  16. def find_palette(point):
  17. rgb_code_dictionary = {
  18. (255, 255, 255): 0,
  19. (228, 228, 228): 1,
  20. (136, 136, 136): 2,
  21. (34, 34, 34): 3,
  22. (255, 167, 209): 4,
  23. (229, 0, 0): 5,
  24. (229, 149, 0): 6,
  25. (160, 106, 66): 7,
  26. (229, 217, 0): 8,
  27. (148, 224, 68): 9,
  28. (2, 190, 1): 10,
  29. (0, 211, 211): 11,
  30. (0, 131, 199): 12,
  31. (0, 0, 234): 13,
  32. (207, 110, 228): 14,
  33. (130, 0, 128): 15
  34. }
  35.  
  36. def distance(c1, c2):
  37. (r1, g1, b1) = c1
  38. (r2, g2, b2) = c2
  39. return math.sqrt((r1 - r2) ** 2 + (g1 - g2) ** 2 + (b1 - b2) ** 2)
  40.  
  41. colors = list(rgb_code_dictionary.keys())
  42. closest_colors = sorted(colors, key=lambda color: distance(color, point))
  43. closest_color = closest_colors[0]
  44. code = rgb_code_dictionary[closest_color]
  45. return code
  46.  
  47.  
  48. s = requests.Session()
  49. s.mount('https://www.reddit.com', HTTPAdapter(max_retries=5))
  50. s.headers["User-Agent"] = "PlacePlacer"
  51. r = s.post("https://www.reddit.com/api/login/{}".format(username),
  52. data={"user": username, "passwd": password, "api_type": "json"})
  53. s.headers['x-modhash'] = r.json()["json"]["data"]["modhash"]
  54.  
  55.  
  56. def place_pixel(ax, ay, new_color):
  57. message = "Probing absolute pixel {},{}".format(ax, ay)
  58.  
  59. while True:
  60. r = s.get("http://reddit.com/api/place/pixel.json?x={}&y={}".format(ax, ay), timeout=5)
  61. if r.status_code == 200:
  62. data = r.json()
  63. break
  64. else:
  65. print("ERROR: ", r, r.text)
  66. time.sleep(5)
  67.  
  68. old_color = data["color"] if "color" in data else 0
  69. if old_color == new_color:
  70. print("{}: skipping, color #{} set by {}".format(message, new_color, data[
  71. "user_name"] if "user_name" in data else "<nobody>"))
  72. time.sleep(.25)
  73. else:
  74. print("{}: Placing color #{}".format(message, new_color, ax, ay))
  75. r = s.post("https://www.reddit.com/api/place/draw.json",
  76. data={"x": str(ax), "y": str(ay), "color": str(new_color)})
  77.  
  78. secs = float(r.json()["wait_seconds"])
  79. if "error" not in r.json():
  80. message = "Placed color, waiting {} seconds. {}% complete."
  81. else:
  82. message = "Cooldown already active - waiting {} seconds. {}% complete."
  83. waitTime = int(secs) + 2
  84. while(waitTime > 0):
  85. m = message.format(waitTime, percent)
  86. time.sleep(1)
  87. waitTime -= 1
  88. if waitTime > 0:
  89. print(m, end=" \r")
  90. else:
  91. print(m)
  92.  
  93. if "error" in r.json():
  94. place_pixel(ax, ay, new_color)
  95.  
  96. # From: http://stackoverflow.com/questions/27337784/how-do-i-shuffle-a-multidimensional-list-in-python
  97. def shuffle2d(arr2d, rand=random):
  98. """Shuffes entries of 2-d array arr2d, preserving shape."""
  99. reshape = []
  100. data = []
  101. iend = 0
  102. for row in arr2d:
  103. data.extend(row)
  104. istart, iend = iend, iend+len(row)
  105. reshape.append((istart, iend))
  106. rand.shuffle(data)
  107. return [data[istart:iend] for (istart,iend) in reshape]
  108.  
  109. while True:
  110. print("starting image placement for img height: {}, width: {}".format(img.height, img.width))
  111. arr2d = shuffle2d([[[i,j] for i in range(img.width)] for j in range(img.height)])
  112. total = img.width * img.height
  113. checked = 0
  114. for y in range(img.width ):
  115. for x in range(img.height ):
  116. xx = arr2d[x][y]
  117. pixel = img.getpixel((xx[0], xx[1]))
  118.  
  119. if pixel[3] > 0:
  120. pal = find_palette((pixel[0], pixel[1], pixel[2]))
  121.  
  122. ax = xx[0] + origin[0]
  123. ay = xx[1] + origin[1]
  124.  
  125. place_pixel(ax, ay, pal)
  126. checked += 1
  127. percent = round((checked/total) * 100, 2)
  128. message = "All pixels placed, sleeping {}s..."
  129. waitTime = 60
  130. while(waitTime > 0):
  131. m = message.format(waitTime)
  132. time.sleep(1)
  133. waitTime -= 1
  134. if waitTime > 0:
  135. print(m, end=" \r")
  136. else:
  137. print(m)
Add Comment
Please, Sign In to add comment