Coding

Programming More Trees in Python! (Recursion & Artwork)

  • 00:00:00 [Music]
  • 00:00:05 [Music]
  • 00:00:11 hey how's it going everyone so I'm a
  • 00:00:14 little bit late but welcome back to
  • 00:00:16 another video in this video we're going
  • 00:00:18 to continue where we left off the last
  • 00:00:19 one and so in the last video we
  • 00:00:21 programmed this little cool drawing
  • 00:00:23 using Python in this video we're going
  • 00:00:25 to do a couple more cool visualizations
  • 00:00:27 of trees and Python if you missed it in
  • 00:00:29 the last video I do have a contest going
  • 00:00:31 on right now so if you tagged me in a
  • 00:00:33 tweet or an Instagram poster story with
  • 00:00:35 some sort of cool tree visualization
  • 00:00:38 that you programmed I will selectively
  • 00:00:40 pick ones that I really like and donate
  • 00:00:42 trees on your behalf so if you
  • 00:00:43 participate in that that would be
  • 00:00:44 awesome all right let's do some coding
  • 00:00:46 let's try to make a more realistic
  • 00:00:48 looking tree and to do that we'll be
  • 00:00:50 using recursion so basically if we think
  • 00:00:53 about how a tree actually looks you know
  • 00:00:55 you have the mean like trunk and that
  • 00:00:58 branches off and then each of those
  • 00:01:00 branches have a bunch of branches and so
  • 00:01:02 on and this perfectly like kind of
  • 00:01:04 emulates what we use recursion for so
  • 00:01:07 I'm going to show some stuff on the
  • 00:01:08 screen basically this is a simple branch
  • 00:01:11 we go straight then right then straight
  • 00:01:14 then back and left and back and back
  • 00:01:17 that's a single branch if we keep
  • 00:01:20 repeating more and more branches we'll
  • 00:01:23 get something that looks more and more
  • 00:01:25 like a tree so now we're going to branch
  • 00:01:26 twice okay it's starting to look a
  • 00:01:30 little bit better let's branch a third
  • 00:01:32 time
  • 00:01:33 cool and it's pretty sweet like with
  • 00:01:36 just me changing one number we're
  • 00:01:39 getting a more sophisticated branching
  • 00:01:42 thing okay so maybe that looks more like
  • 00:01:47 a tree in your eyes I think I started
  • 00:01:49 seeing that looks like I'm in Boston it
  • 00:01:51 looks like a tree maybe in the winter
  • 00:01:53 no Leafs or anything and let's go one
  • 00:01:56 more layer deep okay so what is going on
  • 00:02:03 well we have this tree function and
  • 00:02:06 basically what this tree function is
  • 00:02:07 doing is going straight then branching
  • 00:02:10 right and left and on each of those
  • 00:02:12 branches it also calls the tree function
  • 00:02:16 so each of those branches do the same
  • 00:02:18 thing they go straight then right and
  • 00:02:20 left and it's going to keep doing that
  • 00:02:22 until this depth hits the branch cap so
  • 00:02:26 this is called our base condition so as
  • 00:02:29 I change this number of this branch
  • 00:02:30 count we got more sophisticated looking
  • 00:02:33 trees because it was allowed to branch
  • 00:02:35 more until it hit that base condition
  • 00:02:37 and once it hits the base condition kind
  • 00:02:39 of the recursion unravels and it
  • 00:02:41 eventually just completes execution
  • 00:02:43 because no more recursive calls of tree
  • 00:02:46 are being made the reason that branches
  • 00:02:48 were getting smaller is because each
  • 00:02:49 time I called this tree we multiplied
  • 00:02:52 this shrink factor in which made it 80%
  • 00:02:54 length of what it was before
  • 00:02:56 and doing this kind of makes it look
  • 00:02:58 more like a tree I guess looks okay
  • 00:03:01 let's move into what I did for a more
  • 00:03:04 impressive looking recursive tree so in
  • 00:03:06 my github I have a file called realistic
  • 00:03:09 tree recursive and that looks like this
  • 00:03:12 so it starts off brown it gets a little
  • 00:03:15 bit skinnier then it turns green like we
  • 00:03:17 have nice Leafs and it is doing some
  • 00:03:20 crazy stuff branching it's going really
  • 00:03:21 fast to even just comparing to what we
  • 00:03:24 just showed it looks more authentic
  • 00:03:26 already and the big reason for why it
  • 00:03:29 looks a little bit more realistic is
  • 00:03:31 that there's some randomness happening
  • 00:03:34 it's not always branching the same way
  • 00:03:36 like trees are not perfect
  • 00:03:37 have to add a little bit of randomness
  • 00:03:38 to your recursion to make it look more
  • 00:03:41 tree-like so that's kind of what's going
  • 00:03:43 on in here I'm not going to go through
  • 00:03:45 this whole animation because it takes a
  • 00:03:47 while but basically it's randomly
  • 00:03:51 selecting whether it should branch but
  • 00:03:52 it's not changing too too much otherwise
  • 00:03:54 how did I change the code up from base
  • 00:03:57 recursive to get kind of to what you see
  • 00:03:59 here well one thing I did was add more
  • 00:04:01 branches so now instead of branching
  • 00:04:03 twice it branches four times each time
  • 00:04:07 it branches it turns a little bit it's
  • 00:04:09 not a severe branch but I do a lot more
  • 00:04:13 so as you see there's a branch count of
  • 00:04:15 seven same string factor of eight zero
  • 00:04:17 point a eighty percent each new branch
  • 00:04:19 one of the key things here is this
  • 00:04:23 randomness so basically I generate a
  • 00:04:25 random number and if it's greater than
  • 00:04:27 0.33 so those random numbers between
  • 00:04:30 zero and one so about 66% of the time it
  • 00:04:33 will be above 0.33 that case I do the
  • 00:04:38 recursion I continue with the recursion
  • 00:04:40 but if it's less than 0.2 the three I
  • 00:04:43 break early and then I have my normal
  • 00:04:44 base case here and I kind of reverse the
  • 00:04:47 order where it counts downwards and as
  • 00:04:51 opposed to upwards in that base
  • 00:04:52 recursive one the reason I did that was
  • 00:04:55 I used the depth here to decide how
  • 00:04:58 thick my branch should be so if you see
  • 00:05:01 that animation again it starts off a bit
  • 00:05:04 thicker and then it gets skinnier just
  • 00:05:05 kind of like branches do so this is
  • 00:05:07 depth seven and it's also equal to width
  • 00:05:10 seven then it gets with six with five so
  • 00:05:14 it looks more authentic because you're
  • 00:05:16 you smartly using this depth in your
  • 00:05:19 turtle width as well once you're a
  • 00:05:22 certain depth into the tree
  • 00:05:24 you go from a brown color to a green
  • 00:05:26 color so this helps me do that one
  • 00:05:28 little secret thing is because random
  • 00:05:32 dot random if you run this every time
  • 00:05:34 you might get really unlucky on the
  • 00:05:36 first two branches you might get a
  • 00:05:38 number that's less than 0.33 so the
  • 00:05:40 whole tree would fail I basically seed
  • 00:05:44 it so this makes the randomness always
  • 00:05:46 behave the same it's it's a random
  • 00:05:48 iteration but I can like always
  • 00:05:50 those random results so I cede it to a
  • 00:05:53 random value and I basically just seeded
  • 00:05:55 it I played around and tested this and
  • 00:05:57 seeded it to a value that I liked all
  • 00:05:59 right for the next part of this video
  • 00:06:00 we're gonna switch things up a bit and
  • 00:06:01 actually show you can use Python to
  • 00:06:02 create really cool-looking paintings I
  • 00:06:04 first did this project for one of my
  • 00:06:06 classes I was taking as a master student
  • 00:06:08 at MIT it was a class called advanced
  • 00:06:11 computational photography and you know
  • 00:06:13 we did all sorts of cool image
  • 00:06:14 manipulation throughout the class but
  • 00:06:16 ultimately my final project was this
  • 00:06:19 painterly rendering so the basic process
  • 00:06:21 is you take some sort of cool photo you
  • 00:06:23 work some Python magic and then
  • 00:06:25 ultimately you output some sort of
  • 00:06:26 rendering of that same photo that looks
  • 00:06:29 like a painting and so the first step in
  • 00:06:31 this is that we need to find some cool
  • 00:06:33 photos and because this is a team trees
  • 00:06:35 video I specifically want some cool
  • 00:06:36 photos of trees so for that we're going
  • 00:06:38 to go to this Instagram photographer
  • 00:06:40 kyle Rojo's page his tag is Cairo Joe so
  • 00:06:44 if you like what you see you should
  • 00:06:45 definitely check him out but he has all
  • 00:06:47 sorts of cool nature shots and one thing
  • 00:06:48 I particularly like about his work is
  • 00:06:50 that he includes a lot of the great
  • 00:06:52 state of New Hampshire
  • 00:06:52 my original stomping grounds so if you
  • 00:06:56 take a photo like this you have some
  • 00:06:57 beautiful trees we're gonna turn this
  • 00:06:59 into a painting the algorithm for this
  • 00:07:01 works as follows so we have our photo
  • 00:07:03 and we also take a image of a
  • 00:07:05 brushstroke
  • 00:07:06 and basically what we do is that for
  • 00:07:08 that photo we randomly sample points all
  • 00:07:11 around the image so each time we sample
  • 00:07:14 a point we look at the color of that
  • 00:07:16 point and then on a blank canvas we draw
  • 00:07:21 that brushstroke you just saw in that
  • 00:07:24 spot of that same color and we just keep
  • 00:07:27 repeating this like thousands of times
  • 00:07:29 so eventually by the time we're done
  • 00:07:31 we've sampled every point in the photo
  • 00:07:33 and we have a like painting that
  • 00:07:36 resembles the photo so this is the basic
  • 00:07:38 process but we can kind of keep
  • 00:07:39 improving upon this one thing we can do
  • 00:07:42 is in the areas of high contrast we can
  • 00:07:46 draw smaller brush strokes so that will
  • 00:07:49 bring more clarity to our image the next
  • 00:07:51 thing we can do is we gotta actually
  • 00:07:52 start looking at our image and we can
  • 00:07:54 use image processing techniques to see
  • 00:07:56 the curvature of the edges in our image
  • 00:07:59 and so once we can find those curvatures
  • 00:08:02 we can orientate
  • 00:08:04 our brushstrokes so we're doing the same
  • 00:08:06 process but we can orientate the
  • 00:08:07 brushstrokes in such a way that they
  • 00:08:10 more closely mirror the actual image
  • 00:08:12 once you take all these factors and
  • 00:08:14 bring them together you get some really
  • 00:08:16 cool paintings so I was playing around
  • 00:08:18 with this a lot and here's some of the
  • 00:08:20 artwork I created alright with that I'm
  • 00:08:42 going to end the video here hopefully
  • 00:08:44 you guys enjoyed this stuff definitely
  • 00:08:46 get involved in the contests I mentioned
  • 00:08:47 at the beginning and also definitely
  • 00:08:49 donate to team trees that'd be super
  • 00:08:51 awesome it's so close to getting to 20
  • 00:08:53 million if you did enjoy this video I
  • 00:08:55 mean a lot to me if you hit that
  • 00:08:56 subscribe button and also throw this
  • 00:08:58 video a big like I'm ready back with
  • 00:09:00 some of my traditional kind of long
  • 00:09:02 tutorials probably by the end of the
  • 00:09:04 month the next video is going to be a
  • 00:09:05 another Python pandas data science
  • 00:09:08 library video so make sure to check back
  • 00:09:10 around the end of the month so you don't
  • 00:09:12 miss that but yeah that's all we're
  • 00:09:14 gonna do in this video thank you guys
  • 00:09:15 for watching peace out
  • 00:09:17 [Music]