Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submitting changes #1

Open
wants to merge 2 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added mymovie.avi
Binary file not shown.
56 changes: 14 additions & 42 deletions recursive_art.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
""" TODO: Put your header comment here """
"""
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job addressing the comments, but I don't see any evidence of new functionality.. Even if it didn't work, include what you tried.

Computational Art
Author: John Moreland
"""
import math
import random
from PIL import Image

from PIL import Image, ImageSequence

def build_random_function(min_depth, max_depth):
""" Builds a random function of depth at least min_depth and depth
at most max_depth (see assignment writeup for definition of depth
in this context)

min_depth: the minimum depth of the random function
min_depth: hte minimum depth of the random function
max_depth: the maximum depth of the random function
returns: the randomly generated function represented as a nested list
(see assignment writeup for details on the representation of
these functions)
"""

efl = ["prod","avg","cos_pi","sin_pi","x","y", "dist", "weightedavg"] #elementary function list

if min_depth < 1 and random.choice([0,1]):
return random.choice([["x"],["y"]])

if max_depth < 1:
return random.choice([["x"],["y"]])
else:
randfunc = random.choice(efl) #choose a random function
if randfunc == "prod" or randfunc == "avg" or randfunc == "dist" or randfunc == "weightedavg" : #these functions take two arguments
# print "got prod or avg"
return [randfunc, [build_random_function(min_depth-1, max_depth-1), build_random_function(min_depth-1, max_depth-1)]]
else:
return [randfunc, [build_random_function(min_depth-1, max_depth-1)]]



def evaluate_random_function(f, x, y):
""" Evaluate the random function f with inputs x,y
Representation of the function f is defined in the assignment writeup
Expand Down Expand Up @@ -61,36 +58,25 @@ def evaluate_random_function(f, x, y):
"""

if f[0] == "x":
# print 'x:', x
return x
# return evaluate_random_function(f[1][0],x,y)
elif f[0] == "y":
# print 'y:', y
return y
# return evaluate_random_function(f[1][2],x,y)
elif f[0] == "cos_pi":
# print 'cos_pi:', math.cos(math.pi*evaluate_random_function(f[1][0],x,y))
return math.cos(math.pi*evaluate_random_function(f[1][0],x,y))
elif f[0] == "sin_pi":
# print 'sin_pi:', math.sin(math.pi*evaluate_random_function(f[1][0],x,y))
return math.sin(math.pi*evaluate_random_function(f[1][0],x,y))
elif f[0] == "avg":
# print "avg:", evaluate_random_function(f[1][0],x,y) * evaluate_random_function(f[1][1],x,y) * .5
return (evaluate_random_function(f[1][0],x,y) + evaluate_random_function(f[1][1],x,y)) * .5
elif f[0] == "prod":
# print "prod:", evaluate_random_function(f[1][0],x,y) * evaluate_random_function(f[1][1],x,y) * 1.0
return evaluate_random_function(f[1][0],x,y) * evaluate_random_function(f[1][1],x,y) * 1.0
elif f[0] == "dist":
# print "dist:", evaluate_random_function(f[1][0],x,y) * evaluate_random_function(f[1][1],x,y) * 1.0
return math.sqrt((evaluate_random_function(f[1][0],x,y)*1.0)**2 + (evaluate_random_function(f[1][1],x,y)*1.0)**2)
elif f[0] == "weightedavg": #because x is better anyways
# print "weightedavg:", evaluate_random_function(f[1][0],x,y) * evaluate_random_function(f[1][1],x,y) * 1.0
return ((evaluate_random_function(f[1][0],x,y)*2.0) + evaluate_random_function(f[1][1],x,y)) * .5
else:
print 'Error'
return


def remap_interval(val,
input_interval_start,
input_interval_end,
Expand Down Expand Up @@ -125,7 +111,6 @@ def remap_interval(val,
output_val = (percent*output_delta) + output_interval_start
return output_val


def color_map(val):
""" Maps input value between -1 and 1 to an integer 0-255, suitable for
use as an RGB color code.
Expand All @@ -146,9 +131,8 @@ def color_map(val):
color_code = remap_interval(val, -1, 1, 0, 255)
return int(color_code)


def test_image(filename, x_size=350, y_size=350):
""" Generate test image with random pixels and save as an image file.
""" Generate test image with random pixels and save as an iamge file.

filename: string filename for image (should be .png)
x_size, y_size: optional args to set image dimensions (default: 350)
Expand All @@ -166,8 +150,7 @@ def test_image(filename, x_size=350, y_size=350):

im.save(filename)


def generate_art(filename, x_size=350, y_size=350):
def generate_art(filename, x_size=500, y_size=500):
""" Generate computational art and save as an image file.

filename: string filename for image (should be .png)
Expand All @@ -178,10 +161,9 @@ def generate_art(filename, x_size=350, y_size=350):
green_function = build_random_function(7, 9)
blue_function = build_random_function(7, 9)

print "red_function", red_function
print "green_function", green_function
print "blue_function", blue_function

# print "red_function", red_function
# print "green_function", green_function
# print "blue_function", blue_function

# Create image and loop over all pixels
im = Image.new("RGB", (x_size, y_size))
Expand All @@ -204,7 +186,6 @@ def generate_art_custom(filename, red_function, green_function, blue_function, x
filename: string filename for image (should be .png)
x_size, y_size: optional args to set image dimensions (default: 350)
"""

# Create image and loop over all pixels
im = Image.new("RGB", (x_size, y_size))
pixels = im.load()
Expand All @@ -217,41 +198,32 @@ def generate_art_custom(filename, red_function, green_function, blue_function, x
color_map(evaluate_random_function(green_function, x, y)),
color_map(evaluate_random_function(blue_function, x, y))
)

im.save(filename)


def generate_art_loop(base_file_name, number_of_iterations):
""" Generates a specified number of computational art pieces

base_final_name: string filename for base
number_of_iterations: amount of paintings you want
"""
i=0
while i < number_of_iterations:
for i in range(number_of_iterations):
filename = base_file_name + str(i) + ".png"
generate_art(filename)
i += 1
print "art pieces made:", i , "/", number_of_iterations




if __name__ == '__main__':
import doctest
doctest.testmod()


# Create some computational art!
# TODO: Un-comment the generate_art function call after you
# implement remap_interval and evaluate_random_function
# generate_art("myart.png")
generate_art_loop('frames', 20)

# Test that PIL is installed correctly
# TODO: Comment or remove this function call after testing PIL install
# test_image("noise.png")

generate_art_loop('moreArt', 5)
#create a video by invoking $ avconv -i frames%d.png -vb 20M movie.avi

#found a cool art piece? uncomment the function below and input your color functions!
# generate_art_custom('hqtest', )