Wednesday, September 16, 2009

Python - determine an image's type (regardless of extension)

# iterate through all the files in the current directory
# identify the type of image file (regardless of extension)


import os
import imghdr


# list files in the current working directory 

for f in os.listdir('.'):
    if os.path.isfile(f):
        imgtype = imghdr.what(f)
        if imgtype is None:
            imgtype = "not image"

    print "'" + f + "'" + " is type " + imgtype



#output: (for me)

#'2007 Oct 31 146.jpg' is type jpeg

#
#'2007 Oct 31 147.jpg' is type jpeg
#
#'2007 Oct 31 148.jpg' is type jpeg
#
#'2007 Oct 31 149.image' is type jpeg

#
#'2007 Oct 31 150.jpg' is type jpeg
#
#'big kitty.GI' is type gif
#
#'eric.image' is type jpeg

#
#'LICENSE.txt' is type not image
#
#'NEWS.txt' is type not image
#


No comments:

Post a Comment