SHARE
TWEET

batch_lsb_cleaner

a guest Aug 13th, 2015 267 Never
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #  Requirements:
  5. #  Install Python 2.7 from https://www.python.org/
  6. #  Install Python Imaging Library from http://www.pythonware.com/products/pil/
  7. #  
  8. #  Usage:
  9. #  Open a terminal and navigate to the directory where you saved this script.
  10. #  Run the command:
  11. #  python this_script.py [/input/directory [/output/directory]]
  12. #  If the input or output directories aren't specified, it will default to the current working directory.
  13. #
  14. #  What it does:
  15. #  This script sets the LSB plane in each channel in each image to zero.
  16. #  
  17.  
  18. import sys
  19. import os
  20. from PIL import Image
  21.        
  22. def clean_pixel(pix):
  23.         return tuple([((x>>1)<<1) for x in pix])
  24.  
  25. def clean_image(dir_in, filename, dir_out):
  26.         filepath = os.path.join(dir_in,filename)
  27.         if not os.path.isfile(filepath):
  28.                 return False
  29.         # Load the original image
  30.         try:
  31.                 im = Image.open(filepath)
  32.                 data_in = im.getdata()
  33.         except:
  34.                 return False
  35.         # Generate the clean image
  36.         clean = Image.new(im.mode, im.size)
  37.         data_clean = [clean_pixel(x) for x in data_in]
  38.         clean.putdata(data_clean)
  39.         clean.save(os.path.join(dir_out,"clean_"+filename))
  40.         return True
  41.        
  42. def main():
  43.         if len(sys.argv)>1:
  44.                 dir_in = sys.argv[1]
  45.                 if len(sys.argv)>2:
  46.                         dir_out = sys.argv[2]
  47.                 else:
  48.                         dir_out = dir_in
  49.         else:
  50.                 dir_in = "./"
  51.                 dir_out = dir_in
  52.                
  53.         print "input directory:",dir_in
  54.         print "output directory:",dir_out
  55.         files = os.listdir(dir_in)
  56.         n = 0
  57.         for f in files:
  58.                 if clean_image(dir_in,f,dir_out):
  59.                         n += 1
  60.         print "Cleaned",n,"files"
  61.        
  62.         print "done"
  63.  
  64. if __name__ == '__main__':
  65.         main()
RAW Paste Data
Top