add comments and code cleanup

This commit is contained in:
interfisch 2018-04-01 00:29:17 +02:00
parent a86320aa18
commit 15d54cdce3
1 changed files with 16 additions and 33 deletions

View File

@ -13,78 +13,64 @@ BUFFER_SIZE = 4096
flutwidth = 1920 flutwidth = 1920
flutheight = 1080 flutheight = 1080
width=200
height=200
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT)) s.connect((TCP_IP, TCP_PORT))
c = '000000'
#Load the Image
im = Image.open("ctdo.png") im = Image.open("ctdo.png")
rgb_im = im.convert('RGBA') rgb_im = im.convert('RGBA')
width, height = im.size #get image size
def randomnumber(pseed,pmin,pmax,v1,v2,v3): def randomnumber(pseed,pmin,pmax,v1,v2,v3):
return (pseed*v1+pseed^2*v2+pseed^3*v3)%(pmax-pmin)+pmin return (pseed*v1+pseed^2*v2+pseed^3*v3)%(pmax-pmin)+pmin
newseed=(int)(time.time()/60) newseed=(int)(time.time()/60) #seed based on timestamp (full minutes)
seed=newseed seed=newseed
user=int(sys.argv[1]) #1..n user=int(sys.argv[1]) #1..n
parts=int(sys.argv[2]) #n users parts=int(sys.argv[2]) #n users
if __name__ == '__main__': if __name__ == '__main__':
pixelcoords=[] pixelcoords=[]
for _x in range(width): for _x in range(width):
for _y in range(height): for _y in range(height):
pixelcoords+=[(_x,_y)] pixelcoords+=[(_x,_y)]
random.seed(42) random.seed(42)
random.shuffle(pixelcoords) random.shuffle(pixelcoords) #pseudorandom order
print(pixelcoords) print(pixelcoords)
pixelcoords=pixelcoords[int(len(pixelcoords)/parts)*(user-1):int(len(pixelcoords)/parts)*(user)] #divide in parts for multiple connection
pixelcoords=pixelcoords[int(len(pixelcoords)/parts)*(user-1):int(len(pixelcoords)/parts)*(user)] while True: #Mainloop
while True:
starttime=time.time() starttime=time.time()
commandsarray=[] commandsarray=[]
seed=newseed seed=newseed
print("Seed: "+str(seed)) print("Seed: "+str(seed))
#xv=random.randint(0,flutwidth)
#yv=random.randint(0,flutheight)
#xv=random.randint(width/2,flutwidth-width/2) xv=randomnumber(seed,width/2,flutwidth-width/2, 42,-24,4) #x position of image
#yv=random.randint(height/2,flutheight-height/2) yv=randomnumber(seed,height/2,flutheight-height/2, 43,-23,3) #y ""
xv=randomnumber(seed,width/2,flutwidth-width/2, 42,-24,4)
yv=randomnumber(seed,height/2,flutheight-height/2, 43,-23,3)
print("xv="+str(xv)+ "yv="+str(yv)) print("xv="+str(xv)+ "yv="+str(yv))
_current_command="" _current_command=""
for i,j in pixelcoords: #i,j as xy coordinates from image
for i,j in pixelcoords:
r, g, b,a = rgb_im.getpixel((i, j)) r, g, b,a = rgb_im.getpixel((i, j))
#Do stupid stuffz with p1x3ls
if a<=127: if a<=127:
continue continue
#if r>127: #if r>127:
# continue #skip bright pixels # continue #skip bright pixels
#r=255-r #r=255-r
#_current_command+='PX %d %d %02x%02x%02x\n' % (i+xv,j+yv,r,g,b) #rgb #_current_command+='PX %d %d %02x%02x%02x\n' % (i+xv,j+yv,r,g,b) #rgb
_current_command+='PX %d %d %02x\n' % (i+xv-width/2,j+yv-height/2,r) #monochrome only red channel _current_command+='PX %d %d %02x\n' % (i+xv-width/2,j+yv-height/2,r) #monochrome only red channel
if len(_current_command)>=1400:
if len(_current_command)>=1400: #divide in packets smaller than 1500 byte
commandsarray+=[_current_command.encode()] #append packet commandsarray+=[_current_command.encode()] #append packet
_current_command="" _current_command=""
@ -93,16 +79,14 @@ if __name__ == '__main__':
print("Generated Commands in "+str(time.time()-starttime)+" seconds") print("Generated Commands in "+str(time.time()-starttime)+" seconds")
while seed==newseed: while seed==newseed: #as long as position did not change
try: try:
starttime=time.time() starttime=time.time()
for command in commandsarray: for command in commandsarray:
s.send(command) s.send(command)
print("Send Image in "+str(time.time()-starttime)+" seconds") print("Send Image in "+str(time.time()-starttime)+" seconds")
except BrokenPipeError: except BrokenPipeError: #Brokn Pipe? Reconnect!!
print("BrokenPipeError. Reconnecting") print("BrokenPipeError. Reconnecting")
s.close() s.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -111,5 +95,4 @@ if __name__ == '__main__':
newseed=(int)(time.time()/60) newseed=(int)(time.time()/60)
exit() exit()