import socket
import dpkt
import numpy as np
import ipaddress
from PIL import Image

data = np.zeros((3072,3072,3), dtype=np.uint8)

f = open('ecsc_enhance3.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)

packets = 0

for ts, buf in pcap:
    try:
        packet = socket.inet_ntop(socket.AF_INET6, dpkt.ethernet.Ethernet(buf).data.dst)
        addr = ipaddress.ip_address(packet).exploded

        packet_split = addr.split(':')

        x = int(packet_split[3]) - 550
        y = int(int(packet_split[4])/2)
        r = int(packet_split[5], 16)
        g = int(packet_split[6], 16)
        b = int(packet_split[7], 16)

        data[y,x] = [r,g,b]
    except: pass
    
    packets += 1

    if ((packets % 100000) == 0):
        print(f"{packets} parsed")

        im = Image.fromarray(data)
        im.save(f"testimg_{packets}.png")
