more efficient

This commit is contained in:
interfisch 2018-03-31 13:18:18 +02:00
parent 94f1ab5e21
commit 0d5b013fbc
2 changed files with 51 additions and 17 deletions

BIN
ctdo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@ -2,6 +2,8 @@ import socket
import random import random
import time import time
from PIL import Image from PIL import Image
from random import shuffle
import numpy as np
TCP_IP = '94.45.232.48' TCP_IP = '94.45.232.48'
TCP_PORT = 1234 TCP_PORT = 1234
@ -21,26 +23,58 @@ c = '000000'
im = Image.open("ctdo.png") im = Image.open("ctdo.png")
rgb_im = im.convert('RGB') rgb_im = im.convert('RGB')
commandsarray=[]
if __name__ == '__main__': if __name__ == '__main__':
for a in range(100000): starttime=time.time()
#xv=random.randint(0,flutwidth) #xv=random.randint(0,flutwidth)
#yv=random.randint(0,flutheight) #yv=random.randint(0,flutheight)
xv=flutwidth-width/2 xv=300
yv=height/2 yv=flutheight-height-100
for i in range(width): _current_command=""
pixel_x=i-width/2+xv
for j in range(height): pixelcoords=[]
pixel_y=j-height/2+yv for _x in range(width):
#v1=hex(random.randint(0,255))[2:] for _y in range(height):
#c=v1+v1+v1 pixelcoords+=[(_x,_y)]
r, g, b = rgb_im.getpixel((i, j))
shuffle(pixelcoords)
#c=hex(r)+hex(g)+hex(b)
#c=hex(r)
#s.send("PX {0} {1} {2}\n".format(pixel_x, pixel_y, c)) for i,j in pixelcoords:
s.send( ('PX %d %d %02x%02x%02x\n' % (pixel_x,pixel_y,r,g,b)).encode()) r, g, b = rgb_im.getpixel((i, j))
print(".") #if r>127:
# continue #skip bright pixels
#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\n' % (i+xv,j+yv,r) #monochrome only red channel
if len(_current_command)>=1400:
commandsarray+=[_current_command.encode()] #append packet
_current_command=""
commandsarray+=[_current_command.encode()] #append last packet
print("Generated Commands in "+str(time.time()-starttime)+" seconds")
while True:
try:
starttime=time.time()
for command in commandsarray:
s.send(command)
print("Send Image in "+str(time.time()-starttime)+" seconds")
except BrokenPipeError:
print("BrokenPipeError. Reconnecting")
s.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT)) #reconnect
print("reconnected")
exit() exit()