1. 程式人生 > >Learning Python and being creative. Making art with code.

Learning Python and being creative. Making art with code.

What is this?

It started as a bit of fun late one night on codepen, toying with ways to make images with code. In a really simple way, how can a colour represent a word? Easy… just convert a word to a colour code right?

As it turns out, it wasn’t so easy. Like most developers, I started looking for a JS library that already did this, wishful thinking as it’s not really that

useful. I thought this from Charlie Coleman was pretty cool, but I was more interested in showing a more granular representation. A color for each word, no matter how many letters or words.

Why? And Why Python?

Like many developers I have met along the way, my degree wasn’t in a technical subject. In fact, it was Art. More specifically, sculpture and illustration, but, even though my career is in web development, I still have a creative practice. Code really is just another material to work with. (And no, I’m not saying code is art, because that’s silly.)

Why Python? Well, JS was good, but it was slow and getting slower. I heard python was really fast and I planned to be throwing hundreds of words at it. (Please don’t judge my amateur code too much). I don’t really know much Python, so this was a great excuse to learn.

Planning

After the Codepen version, I came to decide the following:

  • The colours created from the words should get the colour values using every letter, not just the first few.
  • The ‘canvas’ on which the colours sit should be adjustable, I should be able to print a huge image of a whole book, or a small image of my name.
  • The final rendering should be an image, not a web view.

An example: the letters ‘Abcd’ as a word

Given ‘Abcd’ each letter of the word is assigned a value to where it sits in the alphabet like this:

# position   0   1   2   3   4   5  ...alphabet = ["a","b","c","d","e","f",...]

As RGB values only need 3 numbers, we use the remaining letters to increase the values of the numbers representing the first three letters. So…

  • Word is ‘Abcd’
  • Values are [0,1,2]
  • Remaining values are [3]
  • Add [3] to value 1.