There is a standard module for this called imghdr. It's usage is very simple:
from imghdr import what what('~/images/girls.png') 'png'