Guest User

Untitled

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