Advertisement
here2share

# png_function_art.py

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