Monday, December 12, 2016

Next Best Color?

Have you ever wanted to recolor an image with a limited number of colors, replacing each color with the next best color?  Probably not, but I needed it for another project I'm working on, and it end up being a fun exercise.  The basic idea behind this program was to loop through each pixel of an image, and compare it to several colors in a list, and replacing the original color with the color from the list that is closest.

If you read my post "Is it There?" you'll recall I brought up the concept of the distance between two colors.  In the "Is it There" project I used the Euclidean distance, which isn't really that good.  The Euclidean distance just looks at the distance between two points, in this case two points in a three dimensional grid.  Though you can represent colors in an RGB format with a number from 0 to 255 representing how much Red, Green, and Blue are in the color, and you can represent that as a 3 dimensional point, finding the Euclidean distance between two of these points doesn't really work...because color doesn't work like that.

This is where the Delta E (ΔE) comes in.  This is a metric defined by the International Commission on Illumination (a lot of bright people work here), to measure the difference between two colors.  The equation changes between the years and you can find more information about it here.

To illustrate the difference between the Delta E and Euclidean methods I picked a sprite from one of my favorite video games (Terra from Final Fantasy VI).  Here I chose a limited selection of colors and used both the Delta E method, and the Euclidean method to find the the next closet colors.
As you can see, Delta E leads to a much more vibrant image.  The Euclidean distance works, and we are left with a very similar image, but the Delta E gives us brighter greens, closer purples, and just an overall better image.

Now for some fun...

You may recognize this spider to the right as Ochos Locos from my previous post on spiders.  I ran my program, but instead of giving it a set of colors, I had it generate a list of between 15 to 40 randoms colors (and then look at each pixel in the image and replace it with the next closest color.  As it turns out this is a pretty long process, and at sometime in the future I might optimize it.

But! After hours and hours of waiting I bring the work of art that is "Ocho Ochos Locos"

Ocho Ochos Locos
Ocho Ochos Locos

If you're interested in taking a look at the code you can find it here!

Monday, November 21, 2016

Wedding Design Build

When one of my Idahoan friends asked me to 3d print vases to hold electric candles for their wedding I had to dig in.  I went through several iterations, and it ended up being an excellent and interesting piece for their wedding.

They wanted something simple and geometric that would easily blend into the "natury" theme of the wedding (it was at City of Rocks in Alma, Idaho so you can't get too much more natury).  I fiddled around and came up with a basic idea on the right. Please don't look at a closeup of it, it's a pretty crappy print.

It was nice, but I wanted something more symmetrical...I also wanted a better print because I didn't want people to see that about one in public.  I had just started using PLA and was having a lot of problems with slipping, consistency on walls, and overall quality.  Another problem that was plaguing me was this weird bad printing on the right.  After some experimenting I found a setting configuration that worked:

  • Increase temperature to 215C
  • Decrease print speed to 30mm per second
  • Increase the roof thickness
One thing to note is that I'm operating with a pretty junky CTC printer, so if you're new to 3d printing be sure to experiment.  Each machine has it's own setting that work the best.  In the end I was able to make a pretty satisfying print.

Bannanagrams for scale

But this was just step one!  In the middle of this process I was asked if I could paint it to make it look like wood, and after a couple of google searches I discovered Wood PLA! It smells like delicious wood when it prints and actually has a wooden texture.

At this point I received the actual dimensions of the candles to I made the model a little taller
You may be asking yourself why I am only printing one vase right now.  I know I could fit 4 on this bed easily, and I tried, but I ran into a slight problem.  Wood PLA is incredibly....gooey.  No matter what the retraction distance I set, it would always stretch and leave a trail from one model to the other.  This left a very unattractive texture on the side of the vase. Which I did not want.  Sooooo I ended up print them one at a time.  A lot of them.

But it was worth it.  It was a lovely wedding, made only lovelier with my creations!  Enjoy a few glamor shots.

Monday, August 22, 2016

Dr. Eightlegs or: How I Learned to Stop Worrying And Love Arachnids

As a child, spiders were the thing of nightmares to me.  Maybe it's that primitive instinct that developed when we were cavemen telling me that one bite would spell my doom, or maybe it's just the fact that I don't like 8 small legs crawling on my face, but until a few years ago I was paralyzed by the sight of one.  That was until about 2 years ago when I took the time to learn about some of the spider's in Arkansas, and overcome my fears by humanizing them.

This spider, that is currently living in my garage, is called a Cellar Spider.  I've decided to name this one Ochos Locos.  This guy may be able to bite, but he can't really hurt you, in fact he should be your best friend.

Instead of hurting you, Ochos Locos here hurts things that can hurt you.  Cellar Spiders are natural predators to Black Widows, Brown Recluses, and Hobo Spiders.  Not only that, but spiders know how to party.  When threatened Ochos Locos will start vibrating around his web and become a blur.  That's right, with his last breath he plans on dancing (though if they vibrate for too long they will run away).

Of course I don't let any spiders set up camp in my room (for fear of one crawling on my face while I sleep, and inadvertently causing me to punch myself in the face), I let them hang around the garage and in some corners around the house to get rid of the aforementioned venomous spiders, as well as other insects and pests I'd rather not have in here.

You may be asking yourself, why not just get some bug bombs and kill them all?  Well, poison will work for a while, but at some point the spiders are going to come back.  One of the reasons why I don't really see many venomous spiders in my house right now is because of the higher proportion of Cellar Spider to venomous spiders, but after a bug bomb that proportion would be reset.  I would see even more venomous spiders because without a predator, they would find it much easier to "live long and prosper".

With this in mind I've let my friendly neighborhood Cellar Spiders live unmolested, and whenever I find one in the house, I relocate it outside.  If I find a spider that has been doing its job well (killing things I don't want in the house, and not coming into the house) then they I sometimes name them, and that's how Ochos Locos came to be.

Hopefully now you'll think a second before you smash the next spider you find, and potentially name it, and if you want to learn more about your newest spider friend, check out the Wikipedia Article!

Monday, August 15, 2016

Is it there?

My mom, always grabs her mail, and she always puts it in the same place, but sometimes she forgets to mention it to me.  On occasion, when a piece of mail gets around my mail forwarding, then I'd like to know it exists.  So the last time I visited home I set up a webcam connected to my mom's computer, positioned directly above her mail spot.  From there all that was left was to set up a simple Python script to check to see if there is mail there.

I wanted something that works, and not the most complicated solution.  Grabbing the picture wasn't too hard, I tried a few methods I found online, but couldn't get one to work (this time I'm programming on Windows instead of Linux, so I may have had some issues there), but I ended up finding VideoCapture and with a few lines of code I got it working.

From there I used Pillow to scale down the image to a single pixel(think of taking the average of all the colors of an image).  This pixel is represented by a series of three numbers between 0 and 255 representing its RGB values.  For example white is (255,255,255), black is (0,0,0), blue is (0,0,255), etc. 

Now I just needed to compare that color to a stored color. I've worked with colors in Python before, and if you're thinking of the doing something with colors I would suggest using the colormath package.  Here I went with the quick and dirty and just calculated the Euclidean Distance between the two colors: d = sqrt((a1-b1)2+(a2-b2)2+(a3-b3)2).  For example the distance between lime green (34,255,0) and pink (255, 204, 229) is about 322.

If the distance (d) between the two colors exceeds a predetermined threshold, then the script sends an email to me with the original picture, and I know to bring it up in our next talk!

If you want to take a look at the code, take a look at it on my Github.

Wednesday, February 10, 2016

Why is Chekhov's Gun so Satisfying

Recently I've been reading the Expanse series by James S.A. Corey (and if you haven't I suggest you take a look at the first book, the prologue is generally enough to get you hooked).  One of the things I've noticed in his books are a very satisfying and successful use of "Chekhov's Gun" and it got me thinking about what makes them so satisfying. Chekhov's Gun is a common trope used in writing that was originally coined by Anton Chekhov.  He said that:
"Remove everything that has no relevance to the story. If you say in the first chapter that there is a rifle hanging on the wall, in the second or third chapter it absolutely must go off. If it's not going to be fired, it shouldn't be hanging there" Anton Chekhov (1)
Checkhov describes it very simplistically, if it's there, it had better do something or why did you put it there?  For example some charm a character's parents gave them in the first chapter could turn out to be a key later on.  Someone who is unaware of Chekhov's Gun will enjoy the "Aha!" moment when they realize it's real use, but what about people who are aware of it?  Those people that realize that the moment the charm is introduced, that it will have some importance later in the story?

To me the reveal is what defines a good Chekhov's Gun.  At some point during the story a problem is going to present itself.  If you can craft a story where the reader finds herself realizing the importance of some object at the same time the protagonist does, it creates a very satisfying moment.  The reader not only gets the enjoyment of solving a puzzle, but also feels a stronger tie to the protagonist who realized the same thing (the character thinks the same way I do, I can relate more to them).  The same can be applied to the antagonist.  If the reader is able to piece together something bad that the antagonist can do right before he does it, the reader get a great "that son of a bitch" moment.

Chekhov's Gun has been one of my favourite tropes and I'm looking forward to picked out more in the Expanse series.  Hopefully now you can too!'s_gun

  1. S. Shchukin, Memoirs (1911)