Advertisement
Guest User

Untitled

a guest
Feb 7th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #This program will scan a directory and all it's subdirectories for corrupted jpg, png, gif, and bmp images and collect them in a Catch folder
  2.  
  3. #To run this program you will need to install Python 2.7 and PILLOW
  4. #Once installed save this file in a notepad document with the .py extension
  5. #Than run cmd.exe and type the following: C:\Python27\python.exe "C:\Directory this is saved in\this.py" "C:\Directory to be scanned"
  6. #You must make a folder called Catch in your root C:\ directory for the corrupted images to be collected in
  7.  
  8.  
  9. #!/usr/bin/env python2
  10. # -*- coding: utf-8 -*-
  11. # vi:ts=4 sw=4 et
  12.  
  13. # Okay, this code is a bit ugly, with a few "anti-patterns" and "code smell".
  14. # But it works and I don't want to refactor it *right now*.
  15.  
  16. # TODO:
  17. # * Refactor it a little
  18. # * Add support for custom filename filter (instead of the hardcoded one)
  19.  
  20. #Big thanks to denilsonsa for writing most of this code at https://bitbucket.org/denilsonsa/small_scripts/src/542edd54d290d476603e939027ca654b25487d85/jpeg_corrupt.py?at=default
  21.  
  22.  
  23. import getopt
  24. import fnmatch
  25. import re
  26. import os
  27. import os.path
  28. import sys
  29. import PIL.Image
  30. from shutil import move
  31.  
  32. available_parameters = [
  33. ("h", "help", "Print help"),
  34. ("v", "verbose", "Also print clean files"),
  35. ]
  36.  
  37.  
  38. class ProgramOptions(object):
  39. """Holds the program options, after they are parsed by parse_options()"""
  40.  
  41. def __init__(self):
  42. self.globs = ['*.jpg', '*.jpe', '*.jpeg', '*.gif', '*.png', '*.bmp']
  43. self.glob_re = re.compile('|'.join(
  44. fnmatch.translate(g) for g in self.globs
  45. ), re.IGNORECASE)
  46.  
  47. self.verbose = False
  48. self.args = []
  49.  
  50.  
  51. def print_help():
  52. global opt
  53. scriptname = os.path.basename(sys.argv[0])
  54. print(f'Usage: {0} [options] files_or_directories'.format(scriptname))
  55. print('Recursively checks for corrupt image files')
  56. print('')
  57. print('Options:')
  58. long_length = 2 + max(len(long) for x,long,y in available_parameters)
  59. for short, long, desc in available_parameters:
  60. if short and long:
  61. comma = ", "
  62. else:
  63. comma = " "
  64.  
  65. if short == "":
  66. short = " "
  67. else:
  68. short = "-" + short[0]
  69.  
  70. if long:
  71. long = "--" + long
  72.  
  73. print(f'{0}{1}{2:{3}} {4}'.format(short,comma,long,long_length, desc))
  74.  
  75. print('')
  76. print('Currently (it is hardcoded), it only checks for these files:')
  77. print(f' '.join(opt.globs))
  78.  
  79.  
  80. def parse_options(argv, opt):
  81. """argv should be sys.argv[1:]
  82. opt should be an instance of ProgramOptions()"""
  83.  
  84. try:
  85. opts, args = getopt.getopt(
  86. argv,
  87. "".join(short for short,x,y in available_parameters),
  88. [long for x,long,y in available_parameters]
  89. )
  90. except getopt.GetoptError as e:
  91. print(str(e))
  92. print('Use --help for usage instructions.')
  93. sys.exit(2)
  94.  
  95. for o,v in opts:
  96. if o in ("-h", "--help"):
  97. print_help()
  98. sys.exit(0)
  99. elif o in ("-v", "--verbose"):
  100. opt.verbose = True
  101. else:
  102. print(f'Invalid parameter: {0}'.format(o))
  103. print('Use --help for usage instructions.')
  104. sys.exit(2)
  105.  
  106. opt.args = args
  107. if len(args) == 0:
  108. print('Missing filename')
  109. print('Use --help for usage instructions.')
  110. sys.exit(2)
  111.  
  112.  
  113. def is_corrupt(imagefile):
  114. """Returns None if the file is okay, returns an error string if the file is corrupt."""
  115. #http://stackoverflow.com/questions/1401527/how-do-i-programmatically-check-whether-an-image-png-jpeg-or-gif-is-corrupted/1401565#1401565
  116. try:
  117. im = PIL.Image.open(imagefile)
  118. im.verify()
  119. except Exception as e:
  120. return str(e)
  121. return None
  122.  
  123.  
  124. def check_files(files):
  125. """Receives a list of files and check each one."""
  126. global opt
  127. i = 0
  128. for f in files:
  129. # Filtering JPEG, GIF, PNG, and BMP images
  130. i=i+1
  131. if opt.glob_re.match(f):
  132. status = is_corrupt(f)
  133. if opt.verbose and status is None:
  134. status = "Ok"
  135. if status:
  136. file = "{0}".format(f, status)
  137. print(file)
  138. shorthand = file.rsplit('\\', 1)
  139. extention =shorthand[1]
  140. fullFileName = "C:\\Catch" + "\\" + extention
  141. print(f'{extention}. {fullFileName}, ')
  142. move(file, fullFileName)
  143.  
  144.  
  145. def main():
  146. global opt
  147. opt = ProgramOptions()
  148. parse_options(sys.argv[1:], opt)
  149.  
  150. for pathname in opt.args:
  151. print(pathname)
  152. if os.path.isfile(pathname):
  153. check_files([pathname])
  154. elif os.path.isdir(pathname):
  155. for dirpath, dirnames, filenames in os.walk(pathname):
  156. check_files(os.path.join(dirpath, f) for f in filenames)
  157. else:
  158. print(f'ERROR: {0} is neither a file or a dir.'.format(pathname))
  159.  
  160.  
  161. if __name__ == "__main__":
  162. main()
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement