Guest User

Untitled

a guest
Apr 2nd, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import math
  2. import sys
  3. import time
  4.  
  5. import requests
  6. from PIL import Image
  7. from requests.adapters import HTTPAdapter
  8.  
  9. img = Image.open(sys.argv[1])
  10. origin = (int(sys.argv[2]), int(sys.argv[3]))
  11. username = sys.argv[4]
  12. password = sys.argv[5]
  13.  
  14.  
  15. def find_palette(point):
  16. rgb_code_dictionary = {
  17. (255, 255, 255): 0,
  18. (228, 228, 228): 1,
  19. (136, 136, 136): 2,
  20. (34, 34, 34): 3,
  21. (255, 167, 209): 4,
  22. (229, 0, 0): 5,
  23. (229, 149, 0): 6,
  24. (160, 106, 66): 7,
  25. (229, 217, 0): 8,
  26. (148, 224, 68): 9,
  27. (2, 190, 1): 10,
  28. (0, 211, 211): 11,
  29. (0, 131, 199): 12,
  30. (0, 0, 234): 13,
  31. (207, 110, 228): 14,
  32. (130, 0, 128): 15
  33. }
  34.  
  35. def distance(c1, c2):
  36. (r1, g1, b1) = c1
  37. (r2, g2, b2) = c2
  38. return math.sqrt((r1 - r2) ** 2 + (g1 - g2) ** 2 + (b1 - b2) ** 2)
  39.  
  40. colors = list(rgb_code_dictionary.keys())
  41. closest_colors = sorted(colors, key=lambda color: distance(color, point))
  42. closest_color = closest_colors[0]
  43. code = rgb_code_dictionary[closest_color]
  44. return code
  45.  
  46.  
  47. s = requests.Session()
  48. s.mount('https://www.reddit.com', HTTPAdapter(max_retries=5))
  49. s.headers["User-Agent"] = "PlacePlacer"
  50. r = s.post("https://www.reddit.com/api/login/{}".format(username),
  51. data={"user": username, "passwd": password, "api_type": "json"})
  52. s.headers['x-modhash'] = r.json()["json"]["data"]["modhash"]
  53.  
  54.  
  55. def place_pixel(ax, ay, new_color):
  56. print("Probing absolute pixel {},{}".format(ax, ay))
  57.  
  58. while True:
  59. r = s.get("http://reddit.com/api/place/pixel.json?x={}&y={}".format(ax, ay), timeout=5)
  60. if r.status_code == 200:
  61. data = r.json()
  62. break
  63. else:
  64. print("ERROR: ", r, r.text)
  65. time.sleep(5)
  66.  
  67. old_color = data["color"] if "color" in data else 0
  68. if old_color == new_color:
  69. print("Color #{} at {},{} already exists (placed by {}), skipping".format(new_color, ax, ay, data[
  70. "user_name"] if "user_name" in data else "<nobody>"))
  71. else:
  72. print("Placing color #{} at {},{}".format(new_color, ax, ay))
  73. r = s.post("https://www.reddit.com/api/place/draw.json",
  74. data={"x": str(ax), "y": str(ay), "color": str(new_color)})
  75.  
  76. secs = float(r.json()["wait_seconds"])
  77. if "error" not in r.json():
  78. print("Placed color - waiting {} seconds".format(secs))
  79. else:
  80. print("Cooldown already active - waiting {} seconds".format(int(secs)))
  81. time.sleep(secs + 2)
  82.  
  83. if "error" in r.json():
  84. place_pixel(ax, ay, new_color)
  85.  
  86.  
  87. def runIt():
  88. for y in range(img.height):
  89. for x in range(img.width):
  90. pixel = img.getpixel((x, y))
  91.  
  92. #if pixel[3] > 0:
  93. pal = find_palette((pixel[0], pixel[1], pixel[2]))
  94.  
  95. ax = x + origin[0]
  96. ay = y + origin[1]
  97.  
  98. place_pixel(ax, ay, pal)
  99. print("Restarting Image Check ..... ")
  100. runIt()
  101.  
  102.  
  103. runIt()
Add Comment
Please, Sign In to add comment