Inspired by this topic, I decided to write a program that solves the Mystery Pic using database queries. It was pretty fun, and the end result was a program that is capable of solving the MPIC in <1 second.
Please excuse any gross looking code that you may encounter in this topic, these are scripts that I only planned on running a few times, and I didn't take much care when crafting them.
Tools
- Python (Pillow, pymongo)
- MongoDB
- Time
Step 1: The Images
Well, I could hardly download every neopets image every time I wanted to solve an mpic, so I started with that. There were about 130,000 in the end, I'm sure that I'm still missing many.
I used a combination of Dr. Sloth and the JN item DB to obtain these images.
This script scrapes every image url from Dr. Sloth:
I coupled this with a few lines on the command line to get a list of all item images and misc images. The JN item DB will happily display all items at once, making the item URLs very easy to scrape.
I then used this script to download all the images, this process wasn't as lengthy as you might think:
After all was said and done, I had a directory with 130,000 images in it, this was the longest and most annoying part by far.
Step 2: The Documents
I needed a way to quickly search the database for images, so for each image, I generated a BSON document that contained a list of all the colors contained in the image. The plan was to match images that contained the same colors as the MPIC.
Here is the script that I used to generate the docs from images, and upload the generated docs to MongoDB:
These generated documents take up about a gig across 3 files, which is pretty manageable compared to the 130,000 images from before.
Step 3: The Profit
The final script I had to write is short and sweet. Download the MPIC hint from the supplied URL, get a list of the colors that it uses, and query the database for any images that contain the same colors:
Which results in a short and sweet final output:
{u'_id': ObjectId('55a58719292f980c548598d4'), u'name': u'plush_kyrii_mutant.gif'} Found in 0.832999944687 seconds