import socket
import time
from time import sleep
from PIL import Image

IP = "100.65.0.2"
PORT = 5005

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

xoff, yoff = 520, 750
image = Image.open("arrow.jpg")

width, height = image.size
pixels = image.load()

def to_coord(num):
    return int(bin(num)[-8:], 2), int(bin(num)[2:-8], 2)


def packet(x, y, r, g, b):
    result = bytes([0, 0])
    result += bytes([*to_coord(x), *to_coord(y), r, g, b])
    return result


def imagepacket(x, y):
    r, g, b = pixels[x,y]
    result = packet(x + xoff, y + yoff, r, g, b)
    return result


packets = [
    imagepacket(x, y)
    for x in range(width)
    for y in range(height)
]

import random
random.shuffle(packets)

i = 0
while True:
    i += 1
    if i % 500 == 0:
        print(f"Drawing: {i}")
    sleep(1)
    for p in packets:
        sock.sendto(p, (IP, PORT))

