multiprocessing
This commit is contained in:
50
pixelflut.py
50
pixelflut.py
@@ -3,19 +3,16 @@ import socket
|
|||||||
import PIL
|
import PIL
|
||||||
from PIL import Image, ImageColor
|
from PIL import Image, ImageColor
|
||||||
import argparse
|
import argparse
|
||||||
import threading
|
import multiprocessing
|
||||||
|
import random
|
||||||
|
from pprint import pprint
|
||||||
from sys import exit
|
from sys import exit
|
||||||
|
|
||||||
sizex = 800
|
sizex = 800
|
||||||
sizey = 600
|
sizey = 600
|
||||||
|
|
||||||
XSPLIT = 5
|
|
||||||
YSPLIT = 5
|
|
||||||
|
|
||||||
threads = []
|
threads = []
|
||||||
|
|
||||||
dirtybox = (0,0,sizex,sizey)
|
|
||||||
|
|
||||||
old_framebuffer = Image.new("RGBA", (sizex, sizey), None)
|
old_framebuffer = Image.new("RGBA", (sizex, sizey), None)
|
||||||
framebuffer = Image.new("RGBA", (sizex, sizey), (0,0,0,0))
|
framebuffer = Image.new("RGBA", (sizex, sizey), (0,0,0,0))
|
||||||
|
|
||||||
@@ -31,15 +28,29 @@ effect = parser.add_mutually_exclusive_group(required=False)
|
|||||||
effect.add_argument("--repeat", action="store_true")
|
effect.add_argument("--repeat", action="store_true")
|
||||||
effect.add_argument("--animate", type=int, const=5, nargs='?', metavar="STEPSIZE")
|
effect.add_argument("--animate", type=int, const=5, nargs='?', metavar="STEPSIZE")
|
||||||
parser.add_argument("--bounce", action="store_true")
|
parser.add_argument("--bounce", action="store_true")
|
||||||
|
parser.add_argument("--debug", action="store_true")
|
||||||
|
size = parser.add_mutually_exclusive_group(required=False)
|
||||||
|
size.add_argument("--size", default=[800, 600], type=int, nargs=2)
|
||||||
|
size.add_argument("--autodetect", action="store_true")
|
||||||
|
parser.add_argument("--threads", type=int, default="5")
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
pprint(args)
|
||||||
|
|
||||||
|
sizex = args.size[0]
|
||||||
|
sizey = args.size[1]
|
||||||
|
|
||||||
|
dirtybox = (0,0,sizex,sizey)
|
||||||
|
|
||||||
|
XSPLIT = args.threads
|
||||||
|
YSPLIT = args.threads
|
||||||
|
|
||||||
if args.bounce and not args.animate:
|
if args.bounce and not args.animate:
|
||||||
print("--bounce requires --animate")
|
print("--bounce requires --animate")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
class DrawThread(multiprocessing.Process):
|
||||||
class DrawThread(threading.Thread):
|
|
||||||
def run(self):
|
def run(self):
|
||||||
xfrom = self._kwargs["xfrom"]
|
xfrom = self._kwargs["xfrom"]
|
||||||
yfrom = self._kwargs["yfrom"]
|
yfrom = self._kwargs["yfrom"]
|
||||||
@@ -51,11 +62,18 @@ class DrawThread(threading.Thread):
|
|||||||
buf = bytearray()
|
buf = bytearray()
|
||||||
|
|
||||||
global framebuffer
|
global framebuffer
|
||||||
for x in range(xfrom,xto):
|
if args.debug:
|
||||||
for y in range(yfrom,yto):
|
for x in range(xfrom,xto):
|
||||||
if framebuffer.getpixel((x,y))[3] != 0:
|
for y in range(yfrom,yto):
|
||||||
value = b"%02X%02X%02X%02X"%framebuffer.getpixel((x,y))
|
if framebuffer.getpixel((x,y))[3] != 0:
|
||||||
buf += b"PX %d %d %b\n"%(x+args.xoffset,y+args.yoffset,value)
|
value = b"%02X%02X%02X%02X"%self._kwargs["color"]
|
||||||
|
buf += b"PX %d %d %b\n"%(x+args.xoffset,y+args.yoffset,value)
|
||||||
|
else:
|
||||||
|
for x in range(xfrom,xto):
|
||||||
|
for y in range(yfrom,yto):
|
||||||
|
if framebuffer.getpixel((x,y))[3] != 0:
|
||||||
|
value = b"%02X%02X%02X%02X"%framebuffer.getpixel((x,y))
|
||||||
|
buf += b"PX %d %d %b\n"%(x+args.xoffset,y+args.yoffset,value)
|
||||||
|
|
||||||
s.send(buf)
|
s.send(buf)
|
||||||
while args.repeat:
|
while args.repeat:
|
||||||
@@ -109,7 +127,11 @@ def draw():
|
|||||||
|
|
||||||
for xchunk in xchunks:
|
for xchunk in xchunks:
|
||||||
for ychunk in ychunks:
|
for ychunk in ychunks:
|
||||||
threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1]}))
|
if args.debug:
|
||||||
|
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255), 255)
|
||||||
|
threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1], "color": color}))
|
||||||
|
else:
|
||||||
|
threads.append(DrawThread(kwargs={"xfrom":xchunk[0], "xto":xchunk[1], "yfrom":ychunk[0], "yto":ychunk[1]}))
|
||||||
|
|
||||||
for thread in threads:
|
for thread in threads:
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user