Guest User

Untitled

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