/Scripts/pilfile.py

https://bitbucket.org/archerz/python
Python | 94 lines | 67 code | 8 blank | 19 comment | 23 complexity | 0527a4ff978b3c81ba0224de60328fb2 MD5 | raw file
  1. #!c:\python27\python.exe
  2. #
  3. # The Python Imaging Library.
  4. # $Id$
  5. #
  6. # a utility to identify image files
  7. #
  8. # this script identifies image files, extracting size and
  9. # pixel mode information for known file formats. Note that
  10. # you don't need the PIL C extension to use this module.
  11. #
  12. # History:
  13. # 0.0 1995-09-01 fl Created
  14. # 0.1 1996-05-18 fl Modified options, added debugging mode
  15. # 0.2 1996-12-29 fl Added verify mode
  16. # 0.3 1999-06-05 fl Don't mess up on class exceptions (1.5.2 and later)
  17. # 0.4 2003-09-30 fl Expand wildcards on Windows; robustness tweaks
  18. #
  19. import site
  20. import getopt, glob, sys
  21. from PIL import Image
  22. if len(sys.argv) == 1:
  23. print "PIL File 0.4/2003-09-30 -- identify image files"
  24. print "Usage: pilfile [option] files..."
  25. print "Options:"
  26. print " -f list supported file formats"
  27. print " -i show associated info and tile data"
  28. print " -v verify file headers"
  29. print " -q quiet, don't warn for unidentified/missing/broken files"
  30. sys.exit(1)
  31. try:
  32. opt, args = getopt.getopt(sys.argv[1:], "fqivD")
  33. except getopt.error, v:
  34. print v
  35. sys.exit(1)
  36. verbose = quiet = verify = 0
  37. for o, a in opt:
  38. if o == "-f":
  39. Image.init()
  40. id = Image.ID[:]
  41. id.sort()
  42. print "Supported formats:"
  43. for i in id:
  44. print i,
  45. sys.exit(1)
  46. elif o == "-i":
  47. verbose = 1
  48. elif o == "-q":
  49. quiet = 1
  50. elif o == "-v":
  51. verify = 1
  52. elif o == "-D":
  53. Image.DEBUG = Image.DEBUG + 1
  54. def globfix(files):
  55. # expand wildcards where necessary
  56. if sys.platform == "win32":
  57. out = []
  58. for file in files:
  59. if glob.has_magic(file):
  60. out.extend(glob.glob(file))
  61. else:
  62. out.append(file)
  63. return out
  64. return files
  65. for file in globfix(args):
  66. try:
  67. im = Image.open(file)
  68. print "%s:" % file, im.format, "%dx%d" % im.size, im.mode,
  69. if verbose:
  70. print im.info, im.tile,
  71. print
  72. if verify:
  73. try:
  74. im.verify()
  75. except:
  76. if not quiet:
  77. print "failed to verify image",
  78. print "(%s:%s)" % (sys.exc_type, sys.exc_value)
  79. except IOError, v:
  80. if not quiet:
  81. print file, "failed:", v
  82. except:
  83. import traceback
  84. if not quiet:
  85. print file, "failed:", "unexpected error"
  86. traceback.print_exc(file=sys.stdout)