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.

64 lines
2.0 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
import argparse
# parser stuff
parser = argparse.ArgumentParser(
prog='FluffyPixelfluter',
description='used to paint Pixelflut canvas',)
parser.add_argument('hostname')
parser.add_argument('port')
parser.add_argument('-i','--image')
parser.add_argument('-r','--random', action='store_true')
parser.add_argument('-c','--cat', action='store_true')
parser.add_argument('-wd','--width', default=0)
parser.add_argument('-ht','--height', default=0)
args = parser.parse_args()
HOST, PORT = args.hostname, int(args.port)
# change start point if requested
starth = int(args.height)
startw = int(args.width)
rnd = args.random
# 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])
# open image to be sent or query a cat image
if args.cat: im = cat(sizew,sizeh)
elif image in args: im = Image.open(args.image)
pixels = list(im.getdata())
width, height = im.size
pixels = [pixels[i * width:(i + 1) * width] for i in range(0,height)]
# 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")