Pastebin PRO Accounts SPRING SPECIAL! For a limited time only get 50% discount on a LIFETIME PRO account! Offer Ends April 8th!
SHARE
TWEET
batch_lsb_cleaner
a guest
Aug 13th, 2015
267
Never
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # Requirements:
- # Install Python 2.7 from https://www.python.org/
- # Install Python Imaging Library from http://www.pythonware.com/products/pil/
- #
- # Usage:
- # Open a terminal and navigate to the directory where you saved this script.
- # Run the command:
- # python this_script.py [/input/directory [/output/directory]]
- # If the input or output directories aren't specified, it will default to the current working directory.
- #
- # What it does:
- # This script sets the LSB plane in each channel in each image to zero.
- #
- import sys
- import os
- from PIL import Image
- def clean_pixel(pix):
- return tuple([((x>>1)<<1) for x in pix])
- def clean_image(dir_in, filename, dir_out):
- filepath = os.path.join(dir_in,filename)
- if not os.path.isfile(filepath):
- return False
- # Load the original image
- try:
- im = Image.open(filepath)
- data_in = im.getdata()
- except:
- return False
- # Generate the clean image
- clean = Image.new(im.mode, im.size)
- data_clean = [clean_pixel(x) for x in data_in]
- clean.putdata(data_clean)
- clean.save(os.path.join(dir_out,"clean_"+filename))
- return True
- def main():
- if len(sys.argv)>1:
- dir_in = sys.argv[1]
- if len(sys.argv)>2:
- dir_out = sys.argv[2]
- else:
- dir_out = dir_in
- else:
- dir_in = "./"
- dir_out = dir_in
- print "input directory:",dir_in
- print "output directory:",dir_out
- files = os.listdir(dir_in)
- n = 0
- for f in files:
- if clean_image(dir_in,f,dir_out):
- n += 1
- print "Cleaned",n,"files"
- print "done"
- if __name__ == '__main__':
- main()
RAW Paste Data
