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:
- Start with an arbitrary brush width.
- Start with an arbitrary resolution.
- Use ImageMagick to shrink the source image to a height equal to resolution * brush width.
- 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.
- Submit the strokes and get an accuracy score from the server.
- Iterate over and over again, using different resolutions, brush sizes, and color thresholds, getting a score each time.
- Try it all again, but scanning vertically instead of horizontally.
- Use the painting with the best score.
I came in third place. The pictures generated by my algorithm are still up on the site: https://joyofpainting.nerderylabs.com/player/jberube
Comments