SHARE
TWEET

LSB_cleaner.py

a guest Aug 13th, 2015 247 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 /path/to/image/file.png
  12. #
  13. #  What it does:
  14. #  This script creates 4 new images from the original.
  15. #  [filename].red.png, [filename].green.png, and [filename].blue.png contain the LSB planes from the original image.
  16. #  [filename].clean.png contains a copy of the original image with the LSB planes all set to zero.
  17. #  
  18.  
  19. import sys
  20. from PIL import Image
  21.  
  22. colors = ["red","green","blue"]
  23.  
  24. def triplet(i, v):
  25.         return ((i==0)*v, (i==1)*v, (i==2)*v)
  26.        
  27. def clean_pixel(pix):
  28.         return tuple([((x>>1)<<1) for x in pix])
  29.  
  30. def main():
  31.         # Load the original image
  32.         im = Image.open(sys.argv[1])
  33.         data_in = im.getdata()
  34.         # Generate the LSB images
  35.         for i in range(3):
  36.                 out = Image.new("RGB", im.size)
  37.                 data_out = [triplet(i,255*(x[i]%2)) for x in data_in]
  38.                 out.putdata(data_out)
  39.                 out.save(sys.argv[1]+"."+colors[i]+".png")
  40.         # Generate the clean image
  41.         clean = Image.new("RGB", im.size)
  42.         data_clean = [clean_pixel(x) for x in data_in]
  43.         clean.putdata(data_clean)
  44.         clean.save(sys.argv[1]+".clean.png")
  45.         return 0
  46.  
  47. if __name__ == '__main__':
  48.         main()
RAW Paste Data
Top