Posted On: Sun, 09/16/2018 - 00:37

Joy of Painting

Someone awesome at our company came up with a contest for us all to do in our free time. An API was presented to us: one endpoint can be used to request a JPEG image of a painting, and another endpoint can be used to submit coordinates and colors for brush strokes corresponding to that image. After submitting strokes, the API returns an accuracy score. The goal was to recreate 200 classic paintings using 500 or fewer brush strokes each. It was a lot of fun to create a program to do this automatically. Mine was done in PHP using ImageMagick for the image processing. My algorithm did basically this:

  1. Start with an arbitrary brush width.
  2. Start with an arbitrary resolution.
  3. Use ImageMagick to shrink the source image to a height equal to resolution * brush width.
  4. Start making a stroke by moving a cursor horizontally from one pixel to the next over the shrunken image. If the colors are similar within a certain threshold, keep the stroke going, otherwise end the stroke and start a new one.
  5. Submit the strokes and get an accuracy score from the server. 
  6. Iterate over and over again, using different resolutions, brush sizes, and color thresholds, getting a score each time.
  7. Try it all again, but scanning vertically instead of horizontally.
  8. Use the painting with the best score.

I came in third place. The pictures generated by my algorithm are still up on the site:


Back to Blog Listing