Initial commit v2

master
Raphaël Vinot 2015-02-22 02:00:47 +01:00
parent 6ee4c5312b
commit 30c7b90032
5 changed files with 136 additions and 0 deletions

30
v2/png_creator.py Normal file
View File

@ -0,0 +1,30 @@
#!/usr/bon/env python
from PIL import Image
import socket
import random
import time
HOST, PORT = "localhost", 9999
nb_images = 10
for i in range(nb_images):
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
img1 = Image.new('RGBA', (100, 100), (r, g, b, 128))
data = img1.tobytes()
try:
# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
# Connect to server and send data
sock.sendall(bytes(data))
time.sleep(2)
finally:
sock.close()

38
v2/png_merger.py Normal file
View File

@ -0,0 +1,38 @@
#!/usr/bon/env python
from PIL import Image
import redis
import datetime
import time
def merge(r):
if not r.exists('new'):
return None
now = datetime.datetime.now().isoformat()
r.rename('new', now)
new_imgs = r.smembers(now)
print('got new images', len(new_imgs))
r.delete(now)
if len(new_imgs) == 1:
image = Image.frombytes('RGBA', (100, 100), list(new_imgs)[0])
image.save(now + '.png', 'png')
else:
images = []
for i in new_imgs:
try:
images.append(Image.frombytes('RGBA', (100, 100), i))
except:
continue
if len(images) == 2:
img_alpha = Image.alpha_composite(*images)
images = [img_alpha]
img_alpha.save(now + '.png', 'png')
if __name__ == "__main__":
r = redis.Redis()
while True:
merge(r)
time.sleep(10)

27
v2/readme.md Normal file
View File

@ -0,0 +1,27 @@
Installation
============
```
pip install -r requirements.txt
```
Usage
=====
Run the listener receiving the images from the clients:
```
python receiver.py
```
Run the script merging the images:
```
python png_merger.py
```
Run the scripts sending the images:
```
python png_creator.py
```

39
v2/receiver.py Normal file
View File

@ -0,0 +1,39 @@
#!/usr/bon/env python
import socketserver
import redis
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
r = redis.Redis()
data = None
while True:
temp = self.request.recv(1024).strip()
if data is None:
data = temp
else:
data += temp
if len(temp) == 0:
break
print("{} sent a packet".format(self.client_address[0]))
r.sadd('new', data)
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()

2
v2/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
redis
pillow