Last active
December 29, 2015 16:48
-
-
Save melwil/7699341 to your computer and use it in GitHub Desktop.
Generates christmas trees.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:UTF-8 -*- | |
####################################################### | |
# # | |
# This program creates chrismas trees. The initial # | |
# purpose of it was to have a template to make pixel # | |
# art trees for the windows of the office at NTNU. # | |
# # | |
# Author: Håvard Slettvold aka. melwil # | |
# # | |
# Dependencies: PyPNG # | |
# # | |
####################################################### | |
import random | |
import png | |
GREEN = (34, 177, 76) | |
BROWN = (128, 64, 0) | |
YELLOW = (253, 216, 2) | |
PINK = (255, 0, 128) | |
RED = (237, 28, 26) | |
# These settings adjust the dimentions of the final tree | |
height = 20 | |
width = 15 | |
# This is to scale the pixels, if set to 1, height and width | |
# will be actual pixels in the image. | |
# If set to higher than 1, it will include a grid pattern. | |
degree_of_magnification = 1 | |
def make_tree(height, width): | |
if width % 2 == 0: | |
width -= 1 | |
tree = [[(255, 255, 255) for i in xrange(width)] for i in xrange(height)] | |
width_of_stem = calc_width_of_stem(width) | |
height_of_stem = calc_height_of_stem(height) | |
height_pointer = height-1 | |
width_pointer = 0 | |
middle = int((width / 2) + 1) | |
# Input stem | |
width_pointer += (width - width_of_stem) / 2 | |
for x in xrange(0, width_of_stem): | |
for y in xrange(0, height_of_stem-1): | |
tree[height_pointer - y][width_pointer + x] = BROWN | |
height_pointer -= height_of_stem - 1 | |
width_pointer = 0 | |
width_of_next_branch = (width - width_of_stem) / 2 | |
# Branches | |
while True: | |
width_of_branch = width_of_next_branch | |
for y in xrange(width_of_branch + int((width_of_stem / 2) + 1)): | |
for x in xrange(width_of_branch + int((width_of_stem / 2) + 1)): | |
tree[height_pointer - y][middle -1 + x ] = get_tree_fill() | |
tree[height_pointer - y][middle -1 - x ] = get_tree_fill() | |
if height_pointer - y == 0: | |
break | |
width_of_branch -= 1 | |
if height_pointer - y == 0: | |
break | |
height_pointer -= (width_of_next_branch) | |
width_of_next_branch -= 1 | |
if height_pointer - width_of_next_branch <= 0: | |
break | |
elif width_of_next_branch == int(width_of_stem / 2) + 1: | |
break | |
return tree | |
def get_tree_fill(): | |
num = random.randint(0, 90) | |
if num < 10: | |
return YELLOW | |
if num < 20: | |
return RED | |
if num < 30: | |
return PINK | |
else: | |
return GREEN | |
def calc_width_of_stem(width_of_tree): | |
width = int(width_of_tree * 0.2) | |
if width < 1: | |
return 1 | |
else: | |
if width % 2 == 0: | |
return width - 1 | |
return width | |
def calc_height_of_stem(height_of_tree): | |
height = int(height_of_tree * 0.2) | |
if height < 1: | |
return 1 | |
return height | |
def magnify(tree, degree): | |
magnified = [] | |
for row in tree: | |
magnified.append([(0, 0, 0)]*((len(row) * (degree + 1) )+1)) | |
for n in xrange(degree): | |
current = [] | |
current.append((0, 0, 0)) | |
for pixel in row: | |
current.extend([pixel]*degree) | |
current.append((0, 0, 0)) | |
magnified.append(current) | |
magnified.append([(0, 0, 0)]*((len(row) * (degree + 1) )+1)) | |
return magnified | |
tree = make_tree(height, width) | |
magnified = tree | |
magnified_width = width | |
magnified_height = height | |
if degree_of_magnification > 1: | |
magnified = magnify(tree, degree_of_magnification) | |
magnified_width = (width * (degree_of_magnification + 1)) + 1 | |
magnified_height = (height * (degree_of_magnification + 1)) + 1 | |
flat_format = [] | |
for row in magnified: | |
current = [] | |
for pixel in row: | |
for value in pixel: | |
current.append(value) | |
if width % 2 == 0: | |
current.extend([255,255,255]) | |
flat_format.append(current) | |
f = open('tree.png', 'wb') | |
w = png.Writer(magnified_width, magnified_height) | |
w.write(f, flat_format) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment