Dummy simplified

This commit is contained in:
Balthasar Reuter
2018-07-02 21:39:21 +02:00
parent fc24c3cc4c
commit 7746cd0c91

View File

@@ -18,8 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import math
import random
from colorsys import hsv_to_rgb
from PIL import Image
@@ -34,8 +33,9 @@ class CameraDummy(Camera):
self.hasPreview = True
self.hasIdle = False
self._size = (1920, 1280)
random.seed()
self._hue = 0
logging.info('Using CameraDummy')
@@ -45,91 +45,7 @@ class CameraDummy(Camera):
def getPicture(self):
return plotColor(buildExpr(), buildExpr(), buildExpr())
# The following codes were taken from
# https://github.com/j2kun/random-art/blob/master/randomart.py to generate some
# psychedelic random pictures for testing purposes.
class X:
def eval(self, x, y):
return x
class Y:
def eval(self, x, y):
return y
class SinPi:
def __init__(self, prob):
self.arg = buildExpr(prob * prob)
def eval(self, x, y):
return math.sin(math.pi * self.arg.eval(x, y))
class CosPi:
def __init__(self, prob):
self.arg = buildExpr(prob * prob)
def eval(self, x, y):
return math.cos(math.pi * self.arg.eval(x, y))
class Times:
def __init__(self, prob):
self.lhs = buildExpr(prob * prob)
self.rhs = buildExpr(prob * prob)
def eval(self, x, y):
return self.lhs.eval(x, y) * self.rhs.eval(x, y)
def buildExpr(prob=0.99):
if random.random() < prob:
return random.choice([SinPi, CosPi, Times])(prob)
else:
return random.choice([X, Y])()
def plotIntensity(exp, pixelsPerUnit=150):
canvasWidth = 2 * pixelsPerUnit + 1
canvas = Image.new("L", (canvasWidth, canvasWidth))
for py in range(canvasWidth):
for px in range(canvasWidth):
# Convert pixel location to [-1,1] coordinates
x = float(px - pixelsPerUnit) / pixelsPerUnit
y = -float(py - pixelsPerUnit) / pixelsPerUnit
z = exp.eval(x, y)
# Scale [-1,1] result to [0,255].
intensity = int(z * 127.5 + 127.5)
canvas.putpixel((px, py), intensity)
return canvas
def plotColor(redExp, greenExp, blueExp, pixelsPerUnit=150):
redPlane = plotIntensity(redExp, pixelsPerUnit)
greenPlane = plotIntensity(greenExp, pixelsPerUnit)
bluePlane = plotIntensity(blueExp, pixelsPerUnit)
return Image.merge("RGB", (redPlane, greenPlane, bluePlane))
self._hue = (self._hue + 1) % 360
r, g, b = hsv_to_rgb(self._hue / 360, .2, .9)
return Image.new('RGB', self._size, (int(r * 255), int(g * 255),
int(b * 255)))