Image processing based off Hawx's Gist https://gist.github.com/hawx/7e5a53fb4b8b34ba0ad5