PageRenderTime 289ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Scripts/pilfile.py

https://github.com/gpolo/pil-py3k
Python | 93 lines | 66 code | 8 blank | 19 comment | 23 complexity | dde4095998a6c1dd7bc462ca454fc7b5 MD5 | raw file
  1. #! /usr/local/bin/python
  2. #
  3. # The Python Imaging Library.
  4. # $Id: pilfile.py 2813 2006-10-07 10:11:35Z fredrik $
  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 getopt, glob, sys
  20. import Image
  21. if len(sys.argv) == 1:
  22. print("PIL File 0.4/2003-09-30 -- identify image files")
  23. print("Usage: pilfile [option] files...")
  24. print("Options:")
  25. print(" -f list supported file formats")
  26. print(" -i show associated info and tile data")
  27. print(" -v verify file headers")
  28. print(" -q quiet, don't warn for unidentified/missing/broken files")
  29. sys.exit(1)
  30. try:
  31. opt, args = getopt.getopt(sys.argv[1:], "fqivD")
  32. except getopt.error as v:
  33. print(v)
  34. sys.exit(1)
  35. verbose = quiet = verify = 0
  36. for o, a in opt:
  37. if o == "-f":
  38. Image.init()
  39. id = Image.ID[:]
  40. id.sort()
  41. print("Supported formats:")
  42. for i in id:
  43. print(i, end=' ')
  44. sys.exit(1)
  45. elif o == "-i":
  46. verbose = 1
  47. elif o == "-q":
  48. quiet = 1
  49. elif o == "-v":
  50. verify = 1
  51. elif o == "-D":
  52. Image.DEBUG = Image.DEBUG + 1
  53. def globfix(files):
  54. # expand wildcards where necessary
  55. if sys.platform == "win32":
  56. out = []
  57. for file in files:
  58. if glob.has_magic(file):
  59. out.extend(glob.glob(file))
  60. else:
  61. out.append(file)
  62. return out
  63. return files
  64. for file in globfix(args):
  65. try:
  66. im = Image.open(file)
  67. print("%s:" % file, im.format, "%dx%d" % im.size, im.mode, end=' ')
  68. if verbose:
  69. print(im.info, im.tile, end=' ')
  70. print()
  71. if verify:
  72. try:
  73. im.verify()
  74. except:
  75. if not quiet:
  76. print("failed to verify image", end=' ')
  77. print("(%s:%s)" % (sys.exc_info()[0], sys.exc_info()[1]))
  78. except IOError as v:
  79. if not quiet:
  80. print(file, "failed:", v)
  81. except:
  82. import traceback
  83. if not quiet:
  84. print(file, "failed:", "unexpected error")
  85. traceback.print_exc(file=sys.stdout)