Advertisement
here2share

# skel_random_art.py

Jun 21st, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. # skel_random_art.py
  2.  
  3. # Skeleton Python Implemention of Random Art
  4. # Adapted by Chris Stone from code by Andrew Farmer
  5.  
  6. dir=r"C:/pydemo/test/"
  7.  
  8. import random, Image, math
  9.  
  10. random.seed()
  11.  
  12. # MISSING CODE: plotIntensity assumes there is a function evalExpr
  13. #   that given an expression, and x and y coordinates, returns a
  14. #   result in [-1,1].
  15.  
  16. def plotIntensity(exp, pixelsPerUnit = 150):
  17.     """Return an grayscale image of the given function"""
  18.     # Create a blank canvas
  19.     canvasWidth = 2 * pixelsPerUnit + 1
  20.     canvas = Image.new("L", (canvasWidth, canvasWidth))
  21.  
  22.     # For each pixel in the canvas...
  23.     for py in range(0, canvasWidth):
  24.         for px in range(0, canvasWidth):
  25.             # Convert pixel location to [-1,1] coordinates
  26.             #  Note that x and y conversions are not exactly the same
  27.             #  because increasing px goes left to right (increasing x)
  28.             #  but increasing py goes from top to bottom (decreasing y)
  29.             x = float(px - pixelsPerUnit) / pixelsPerUnit
  30.             y = -float(py - pixelsPerUnit) / pixelsPerUnit
  31.             # Evaluate the expression at that point
  32.             z = exp.eval(x, y)
  33.             # Scale [-1,1] result to [0,255].
  34.             intensity = int(z * 127.5 + 127.5)
  35.             canvas.putpixel((px,py), intensity)
  36.     return canvas
  37.  
  38.  
  39. def plotColor(redExp, greenExp, blueExp, pixelsPerUnit = 150):
  40.     """Return an image constructed from the three RGB intensity functions"""
  41.     redPlane   = plotIntensity(redExp, pixelsPerUnit)
  42.     greenPlane = plotIntensity(greenExp, pixelsPerUnit)
  43.     bluePlane  = plotIntensity(blueExp, pixelsPerUnit)
  44.     return Image.merge("RGB", (redPlane, greenPlane, bluePlane))
  45.  
  46. class X:
  47.    def eval(self, x, y):
  48.       return x
  49.    
  50.    def __str__(self):
  51.       return "x"
  52.  
  53. class Y:
  54.    def eval(self, x, y):
  55.       return y
  56.    
  57.    def __str__(self):
  58.       return "y"
  59.  
  60. class SinPi:
  61.    def __init__(self, prob):
  62.       self.arg = buildExpr(prob * prob)
  63.    
  64.    def __str__(self):
  65.       return "sin(pi*" + str(self.arg) + ")"
  66.  
  67.    def eval(self, x, y):
  68.       return math.sin(math.pi * self.arg.eval(x,y))
  69.  
  70. class CosPi:
  71.    def __init__(self, prob):
  72.       self.arg = buildExpr(prob * prob)
  73.  
  74.    def __str__(self):
  75.       return "cos(pi*" + str(self.arg) + ")"
  76.  
  77.    def eval(self, x, y):
  78.       return math.cos(math.pi * self.arg.eval(x,y))
  79.  
  80. class Times:
  81.    def __init__(self, prob):
  82.       self.lhs = buildExpr(prob * prob)
  83.       self.rhs = buildExpr(prob * prob)
  84.  
  85.    def __str__(self):
  86.       return str(self.lhs) + "*" + str(self.rhs)
  87.  
  88.    def eval(self, x, y):
  89.       return self.lhs.eval(x,y) * self.rhs.eval(x,y)
  90.  
  91. def buildExpr(prob = 0.99):
  92.    if random.random() < prob:
  93.       return random.choice([SinPi, CosPi, Times])(prob)
  94.    else:
  95.       return random.choice([X, Y])()
  96.  
  97. def makeGray(numPics = 20):
  98.     """Creates n grayscale image files named gray0.png, gray1.png, ..."""
  99.     random.seed()
  100.     for i in range(0,numPics):
  101.         print i, ":"
  102.         grayExp = buildExpr()
  103.         print str(grayExp), "\n"
  104.         image = plotIntensity(grayExp)
  105.         image.save(dir+"Xgray" + str(i) + ".png", "PNG")
  106.  
  107. def makeColor(numPics = 20):
  108.     """Creates n color image files named color0.png, color1.png, ..."""
  109.     random.seed()
  110.     for i in range(0,numPics):
  111.         print i, ":"
  112.         redExp = buildExpr()
  113.         print "red = ", str(redExp)
  114.         greenExp = buildExpr()
  115.         print "green = ", str(greenExp)
  116.         blueExp = buildExpr()
  117.         print "blue = ", str(blueExp), "\n"
  118.         image = plotColor(redExp, greenExp, blueExp)
  119.         image.save(dir+"Xcolor" + str(i) + ".png", "PNG")
  120.  
  121.  
  122. # Generate a bunch of random grayscale and color pictures.
  123. makeGray();
  124. makeColor();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement