pixelflut/eh18-image.py

81 lines
1.9 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
2018-03-31 10:10:00 +00:00
TCP_IP = '94.45.232.48'
TCP_PORT = 1234
BUFFER_SIZE = 4096
flutwidth = 1920
flutheight = 1080
width=200
height=200
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
c = '000000'
im = Image.open("ctdo.png")
rgb_im = im.convert('RGB')
2018-03-31 11:18:18 +00:00
commandsarray=[]
2018-03-31 10:10:00 +00:00
if __name__ == '__main__':
2018-03-31 11:18:18 +00:00
starttime=time.time()
#xv=random.randint(0,flutwidth)
#yv=random.randint(0,flutheight)
xv=300
yv=flutheight-height-100
_current_command=""
pixelcoords=[]
for _x in range(width):
for _y in range(height):
pixelcoords+=[(_x,_y)]
shuffle(pixelcoords)
for i,j in pixelcoords:
r, g, b = rgb_im.getpixel((i, j))
#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")
2018-03-31 10:10:00 +00:00
exit()