pixelflut/eh18-image.py

100 lines
3.0 KiB
Python
Raw Normal View History

2018-03-31 10:10:00 +00:00
import socket
import random
import time
from PIL import Image
2018-03-31 11:18:18 +00:00
from random import shuffle
import numpy as np
import sys
2018-03-31 10:10:00 +00:00
TCP_IP = '94.45.232.48'
TCP_PORT = 1234
BUFFER_SIZE = 4096
flutwidth = 1920
flutheight = 1080
2018-03-31 22:29:17 +00:00
2018-03-31 10:10:00 +00:00
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
image=sys.argv[1] #image name
2018-03-31 22:29:17 +00:00
#Load the Image
im = Image.open(image)
2018-03-31 13:20:35 +00:00
rgb_im = im.convert('RGBA')
2018-03-31 22:29:17 +00:00
width, height = im.size #get image size
2018-03-31 13:20:35 +00:00
def randomnumber(pseed,pmin,pmax,v1,v2,v3):
return (pseed*v1+pseed^2*v2+pseed^3*v3)%(pmax-pmin)+pmin
2018-03-31 22:29:17 +00:00
newseed=(int)(time.time()/60) #seed based on timestamp (full minutes)
2018-03-31 13:20:35 +00:00
seed=newseed
2018-03-31 10:10:00 +00:00
user=int(sys.argv[2]) #1..n
parts=int(sys.argv[3]) #n users
2018-03-31 10:10:00 +00:00
if __name__ == '__main__':
2018-03-31 21:01:33 +00:00
pixelcoords=[]
for _x in range(width):
for _y in range(height):
pixelcoords+=[(_x,_y)]
random.seed(42)
2018-03-31 22:29:17 +00:00
random.shuffle(pixelcoords) #pseudorandom order
2018-03-31 21:01:33 +00:00
print(pixelcoords)
2018-03-31 22:29:17 +00:00
pixelcoords=pixelcoords[int(len(pixelcoords)/parts)*(user-1):int(len(pixelcoords)/parts)*(user)] #divide in parts for multiple connection
2018-03-31 21:01:33 +00:00
2018-03-31 22:29:17 +00:00
while True: #Mainloop
2018-03-31 13:20:35 +00:00
starttime=time.time()
commandsarray=[]
seed=newseed
print("Seed: "+str(seed))
2018-03-31 11:18:18 +00:00
2018-03-31 22:29:17 +00:00
xv=randomnumber(seed,width/2,flutwidth-width/2, 42,-24,4) #x position of image
yv=randomnumber(seed,height/2,flutheight-height/2, 43,-23,3) #y ""
2018-03-31 13:20:35 +00:00
print("xv="+str(xv)+ "yv="+str(yv))
_current_command=""
2018-03-31 11:18:18 +00:00
2018-03-31 22:29:17 +00:00
for i,j in pixelcoords: #i,j as xy coordinates from image
2018-03-31 13:20:35 +00:00
r, g, b,a = rgb_im.getpixel((i, j))
2018-03-31 22:29:17 +00:00
#Do stupid stuffz with p1x3ls
2018-03-31 22:09:41 +00:00
if a<=127:
2018-03-31 13:20:35 +00:00
continue
#if r>127:
# continue #skip bright pixels
2018-03-31 11:18:18 +00:00
2018-03-31 22:52:08 +00:00
_current_command+='PX %d %d %02x%02x%02x\n' % (i+xv-width/2,j+yv-height/2,r,g,b) #rgb
#_current_command+='PX %d %d %02x\n' % (i+xv-width/2,j+yv-height/2,r) #monochrome only red channel
2018-03-31 22:29:17 +00:00
if len(_current_command)>=1400: #divide in packets smaller than 1500 byte
2018-03-31 13:20:35 +00:00
commandsarray+=[_current_command.encode()] #append packet
_current_command=""
2018-03-31 11:18:18 +00:00
2018-03-31 13:20:35 +00:00
commandsarray+=[_current_command.encode()] #append last packet
2018-03-31 11:18:18 +00:00
2018-03-31 13:20:35 +00:00
print("Generated Commands in "+str(time.time()-starttime)+" seconds")
2018-03-31 22:29:17 +00:00
while seed==newseed: #as long as position did not change
2018-03-31 13:20:35 +00:00
try:
starttime=time.time()
for command in commandsarray:
s.send(command)
2018-03-31 11:18:18 +00:00
2018-03-31 13:20:35 +00:00
print("Send Image in "+str(time.time()-starttime)+" seconds")
2018-03-31 22:29:17 +00:00
except BrokenPipeError: #Brokn Pipe? Reconnect!!
2018-03-31 13:20:35 +00:00
print("BrokenPipeError. Reconnecting")
s.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT)) #reconnect
print("reconnected")
2018-03-31 11:18:18 +00:00
2018-03-31 13:20:35 +00:00
newseed=(int)(time.time()/60)
2018-03-31 11:18:18 +00:00
2018-03-31 10:10:00 +00:00
exit()