Fix caching issue while image transformation #4662

* Removed tempfile dependency.
* Fixed caching issue with images.
* Added test for the same.
* Re-factored test.
from pillow