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

project completed #2

Open
wants to merge 1 commit into
base: master
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
19 changes: 19 additions & 0 deletions basicart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
""" Basic Art"""

red_function = ["x"]
green_function = ["y"]
blue_function = ["x"]

im = Image.new("RGB", (x_size, y_size))
pixels = im.load()
for i in range(x_size):
for j in range(y_size):
x = remap_interval(i, 0, x_size, -1, 1)
y = remap_interval(j, 0, y_size, -1, 1)
pixels[i,j] = (
color_map(evaluate_random_function(red_function, x, y))
color_map(evaluate_random_function(green_function, x, y))
color_map(evaluate_random_function(blue_function, x, y)))


im.save(filename)
Binary file added myart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 76 additions & 20 deletions recursive_art.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""TODO: Put your header comment here."""
""" By John Wen"""

import math
import random
from PIL import Image



def build_random_function(min_depth, max_depth):
"""Build a random function.

Expand All @@ -20,8 +23,26 @@ def build_random_function(min_depth, max_depth):
(See the assignment writ-eup for details on the representation of
these functions)
"""
# TODO: implement this
pass
list = []
functionsnoxy = ["avg","prod","cos_pi","sin_pi","square","cube"]
functionsxy = ["x","y"]
functionsall = functionsxy + functionsnoxy
randomfunction = random.choice(functionsnoxy)
if min_depth <= 1:
randomfunction = random.choice(functionsall)
if max_depth == 1:
randomfunction = random.choice(functionsxy)
if randomfunction in ["avg","prod"]:
list.append(randomfunction)
list.append(build_random_function(min_depth-1,max_depth-1))
list.append(build_random_function(min_depth-1,max_depth-1))
if randomfunction in ["cos_pi","sin_pi","square","cube"]:
list.append(randomfunction)
list.append(build_random_function(min_depth-1,max_depth-1))
if randomfunction in ["x","y"]:
list.append(randomfunction)
return list

Choose a reason for hiding this comment

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

For the two if statements from the bottom, I think you can make it as elif statement. That would make the code to jump right to the return if the randomfunction was 'avg' or 'prod'. (Just minor details)
Also, I think list is a reserved keyword for python language, so I would recommend not to use list as variable names. Sometimes you might see unexpected behaviors



def evaluate_random_function(f, x, y):
Expand All @@ -42,9 +63,48 @@ def evaluate_random_function(f, x, y):
-0.5
>>> evaluate_random_function(["y"],0.1,0.02)
0.02
>>> evaluate_random_function(["prod"],4,2)
8
>>> evaluate_random_function(["prod", ["x"], ["y"]], 0, 0)
0
>>> evaluate_random_function(["prod", ["avg", ["x"],["y"]], ["x"]], 2, 2)
4
>>> evaluate_random_function(["avg", ["prod", ["avg", ["x"],["y"]], ["x"]], ["prod", ["x"], ["y"]]], 0, 0)
0.0
>>> evaluate_random_function(["sin_pi", ["avg", ["prod", ["avg", ["x"],["y"]], ["x"]], ["prod", ["x"], ["y"]]]], 0, 0)
0.0
>>> evaluate_random_function(['sin_pi', ['cos_pi', ['cos_pi', ['x']]]], -1, -1)
0.0

Choose a reason for hiding this comment

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

I like how you added more unit tests! Great!

"""
# TODO: implement this
pass
if len(f) == 2:
evaluate_random_function(f[0],evaluate_random_function(f[1],x,y),y)
if len(f) == 3:
evaluate_random_function(f[0],
evaluate_random_function(f[1],x,y),
evaluate_random_function(f[2],x,y))
if f == ["x"]:
# print("return : ", x)
return x
elif f == ["y"]:
# print("return : ", y)
return y
elif f[0] == "prod":
return x * y
elif f[0] == "cos_pi":
# print("return : ", math.cos(math.pi * f[1]))
return math.cos(math.pi * x)
elif f[0] == "sin_pi":
# print("return : ", math.sin(math.pi * f[1]))
return math.sin(math.pi * x)
elif f[0] == "avg":
# print("return : ", (f[1] + f[2] / 2))
return (x + y) / 2
elif f[0] == "square":
# print("return : ", f[1]**2 )
return x ** 2
elif f[0] == "cube":
# print("return : ", f[1]**3 )
return x ** 3

Choose a reason for hiding this comment

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

Great job overall, but I think you can remove the commented print code when submitting



def remap_interval(val,
Expand Down Expand Up @@ -80,8 +140,10 @@ def remap_interval(val,
>>> remap_interval(5, 4, 6, 1, 2)
1.5
"""
# TODO: implement this
pass
inputscale = (val - input_interval_start) / (input_interval_end - input_interval_start)
outputscale = (output_interval_end - output_interval_start)
return inputscale * outputscale + output_interval_start



def color_map(val):
Expand All @@ -103,7 +165,6 @@ def color_map(val):
>>> color_map(0.5)
191
"""
# NOTE: This relies on remap_interval, which you must provide
color_code = remap_interval(val, -1, 1, 0, 255)
return int(color_code)

Expand Down Expand Up @@ -137,9 +198,9 @@ def generate_art(filename, x_size=350, y_size=350):
x_size, y_size: optional args to set image dimensions (default: 350)
"""
# Functions for red, green, and blue channels - where the magic happens!
red_function = ["x"]
green_function = ["y"]
blue_function = ["x"]
red_function = build_random_function(10,15)
green_function = build_random_function(10,15)
blue_function = build_random_function(10,15)

# Create image and loop over all pixels
im = Image.new("RGB", (x_size, y_size))
Expand All @@ -159,13 +220,8 @@ def generate_art(filename, x_size=350, y_size=350):

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")

# Test that PIL is installed correctly
# TODO: Comment or remove this function call after testing PIL install
test_image("noise.png")
# doctest.testmod(verbose=True)
# doctest.run_docstring_examples(evaluate_random_function, globals(), verbose=True)
# doctest.testmod()
# print(evaluate_random_function(["prod", ["x"], ["y"]], 2 ,3))
generate_art("myart.png")