Refactored common image.py code