modified to use pillow rather than PIL