Advertisement
here2share

# plot_rnd_image.py

Jun 13th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import random, math
  2. from PIL import Image
  3.  
  4. random.seed()
  5.  
  6. class X:
  7.    def eval(self, x, y):
  8.       return x
  9.    
  10.    def __str__(self):
  11.       return "x"
  12.  
  13. class Y:
  14.    def eval(self, x, y):
  15.       return y
  16.    
  17.    def __str__(self):
  18.       return "y"
  19.  
  20. class SinPi:
  21.    def __init__(self, prob):
  22.       self.arg = buildExpr(prob * prob)
  23.    
  24.    def __str__(self):
  25.       return "sin(pi*" + str(self.arg) + ")"
  26.  
  27.    def eval(self, x, y):
  28.       return math.sin(math.pi * self.arg.eval(x,y))
  29.  
  30. class CosPi:
  31.    def __init__(self, prob):
  32.       self.arg = buildExpr(prob * prob)
  33.  
  34.    def __str__(self):
  35.       return "cos(pi*" + str(self.arg) + ")"
  36.  
  37.    def eval(self, x, y):
  38.       return math.cos(math.pi * self.arg.eval(x,y))
  39.  
  40. class Times:
  41.    def __init__(self, prob):
  42.       self.lhs = buildExpr(prob * prob)
  43.       self.rhs = buildExpr(prob * prob)
  44.  
  45.    def __str__(self):
  46.       return str(self.lhs) + "*" + str(self.rhs)
  47.  
  48.    def eval(self, x, y):
  49.       return self.lhs.eval(x,y) * self.rhs.eval(x,y)
  50.  
  51. def buildExpr(prob = 0.99):
  52.    if random.random() < prob:
  53.       return random.choice([SinPi, CosPi, Times])(prob)
  54.    else:
  55.       return random.choice([X, Y])()
  56.  
  57. def plotIntensity(exp, pixelsPerUnit = 150):
  58.     canvasWidth = 2 * pixelsPerUnit + 1
  59.     canvas = Image.new("L", (canvasWidth, canvasWidth))
  60.  
  61.     for py in range(canvasWidth):
  62.         for px in range(canvasWidth):
  63.             # Convert pixel location to [-1,1] coordinates
  64.             x = float(px - pixelsPerUnit) / pixelsPerUnit
  65.             y = -float(py - pixelsPerUnit) / pixelsPerUnit
  66.             z = exp.eval(x,y)
  67.  
  68.             # Scale [-1,1] result to [0,255].
  69.             intensity = int(z * 127.5 + 127.5)
  70.             canvas.putpixel((px,py), intensity)
  71.  
  72.     return canvas
  73.  
  74. def plotColor(redExp, greenExp, blueExp, pixelsPerUnit = 150):
  75.     redPlane   = plotIntensity(redExp, pixelsPerUnit)
  76.     greenPlane = plotIntensity(greenExp, pixelsPerUnit)
  77.     bluePlane  = plotIntensity(blueExp, pixelsPerUnit)
  78.     return Image.merge("RGB", (redPlane, greenPlane, bluePlane))
  79.  
  80. def makeImage(numPics = 20):
  81.    with open("eqns.txt", 'w') as eqnsFile:
  82.       for i in range(numPics):
  83.          redExp = buildExpr()
  84.          greenExp = buildExpr()
  85.          blueExp = buildExpr()
  86.  
  87.          eqnsFile.write("img" + str(i) + ":\n")
  88.          eqnsFile.write("red = " + str(redExp) + "\n")
  89.          eqnsFile.write("green = " + str(greenExp) + "\n")
  90.          eqnsFile.write("blue = " + str(blueExp) + "\n\n")
  91.  
  92.          image = plotColor(redExp, greenExp, blueExp)
  93.          image.save("img" + str(i) + ".png", "PNG")
  94.  
  95. #makeImage(50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement