You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB

#!/usr/bin/env python3
import socket
import sys
from itertools import product
import string
import numpy as np
from PIL import Image
from cat import cat
# open image to be sent
image = sys.argv[1]
im = Image.open(image)
pixels = list(im.getdata())
width, height = im.size
pixels = [pixels[i * width:(i + 1) * width] for i in range(0,height)]
HOST, PORT = "px.oeinf.de", 1234
# change start point if requested
starth, startw = 0, 0
if len(sys.argv) > 2: starth = int(sys.argv[2])
if len(sys.argv) > 3: startw = int(sys.argv[3])
rnd = False
if len(sys.argv) > 4: rnd = sys.argv[4] == "rnd"
# Create a socket (SOCK_STREAM means a TCP socket)
# Connect to server and send data
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((HOST, PORT))
# query canvas size
to_send = "size\n"
sock.sendall(bytes(to_send, "utf-8"))
received = str(sock.recv(1024), "utf-8")
sizew = int(received.split(" ")[1])
sizeh = int(received.split(" ")[2])
# create list of stuff and shuffle if requested
widthlist = list(range(0 + startw,min(int(sizew),(width+startw))))
heightlist = list(range(0 + starth, min(int(sizeh),(height+starth))))
if rnd: np.random.shuffle(widthlist)
for w in widthlist:
if rnd: np.random.shuffle(heightlist)
for h in heightlist:
color = '#%02x%02x%02x' % pixels[h-starth][w-startw]
to_send = "px {} {} {}\n".format(w,h,color)
sock.sendall(bytes(to_send, "utf-8"))
sock.close()
print("finished")