Created
September 3, 2016 18:37
-
-
Save fionera/7718a822615d7caa3ec2e4a34a68d133 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, socket | |
import subprocess | |
import sys | |
import time | |
from PIL import Image | |
import threading | |
#HOST = '185.55.125.62' | |
HOST = '185.55.125.70' | |
PORT = 1234 | |
MAXH = 960 | |
MAXW = 1280 | |
iterations = 1 | |
xoffset = 0 | |
yoffset = 0 | |
threads = [] | |
def pixel(x, y, r, g, b, i, a=255): | |
with open("temp", "a") as myfile: | |
if a == 255: | |
myfile.write('PX %d %d %02x%02x%02x\n' % (x, y, r, g, b)) | |
else: | |
myfile.write('PX %d %d %02x%02x%02x%02x\n' % (x, y, r, g, b, a)) | |
def calculateRow(xoffset, yoffset, i, image, h, x): | |
for y in range(h): | |
r, g, b, a = image.getpixel((x, y)) | |
pixel(x + xoffset, y + yoffset, r, g, b, i, a) | |
def calculate(xoffset, yoffset, i, image, w): | |
for x in range(w): | |
thread = threading.Thread(target=calculateRow, args=(xoffset, yoffset, i, image, h, x)) | |
thread.daemon = True | |
thread.start() | |
def send(): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((HOST,PORT)) | |
with open("temp", "r") as myfile: | |
sock.send(myfile.read()) | |
image = Image.open(sys.argv[1]).convert('RGBA') | |
_, _, w, h = image.getbbox() | |
for i in range(iterations): | |
xoffset += 300 | |
yoffset += 200 | |
threads.append(threading.Thread(target=calculate, args=(xoffset, yoffset, i, image, w))) | |
threads[i].daemon = True | |
threads[i].start() | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((HOST,PORT)) | |
while True: | |
calcLiving = False | |
for thread in threads: | |
calcLiving |= thread.isAlive() | |
if not calcLiving: | |
print("Finish") | |
break | |
with open("temp", "r") as myfile: | |
sock.send(myfile.read()) | |
while True: | |
threads.append(threading.Thread(target=send, args=())) | |
threads[-1].daemon = True | |
threads[-1].start() | |
time.sleep(0.2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment