Add a PIL compatibility check

Pillow uses `tobytes` but PIL only has the `tostring` method (which throws a DeprecationWarning on Pillow). This checks and uses the appropriate method.