Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. # Written by kenkeiras
  4. #########################################################################
  5. # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  6. #                    Version 2, December 2004
  7. #
  8. # Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  9. #
  10. # Everyone is permitted to copy and distribute verbatim or modified
  11. # copies of this license document, and changing it is allowed as long
  12. # as the name is changed.
  13. #
  14. #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  15. #   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  16. #
  17. #  0. You just DO WHAT THE FUCK YOU WANT TO.
  18. #########################################################################
  19. #
  20. # Convierte una tupla en un array
  21. def tuple2arr( t ):
  22.     a = []
  23.     for i in t:
  24.         a.append( i )
  25.     return a
  26.  
  27. # Convierte un array en una tupla
  28. def arr2tuple( a ):
  29.     t = ( )
  30.     for i in a:
  31.         t += ( i, )
  32.     return t
  33.  
  34. # Manejo de bits
  35. # Extrae el LSB
  36. def extractLSB(byt):
  37.     result = ( byt ) % 2
  38.     return result
  39.  
  40. # Junta un array de bits en un byte
  41. def composeByte( l ):
  42.    
  43.     out = []
  44.     i = 0
  45.     while i <= 7:
  46.         out.append( l[ i ] )
  47.         i += 1
  48.  
  49.     out = chr(out[ 7 ] + out[ 6 ] * 2 + out[ 5 ] * 4 + out[ 4 ] * 8 + \
  50.          out[ 3 ] * 16 + out[ 2 ] * 32 + out[ 1 ] * 64 + out[ 0 ] * 128 )
  51.  
  52.     return out
  53.  
  54. # Código en si
  55. import sys, os
  56. try:
  57.     from PIL import Image
  58. except:
  59.     print >>sys.stderr, "Necesitas PIL"
  60.     print >>sys.stderr, "[ http://www.pythonware.com/products/pil/ ]"
  61.     exit( 0 )
  62.  
  63. # Comprueba los argumentos
  64. if ( len( sys.argv ) < 3 ):
  65.     print >>sys.stderr, sys.argv[ 0 ], "<imágen> <salida>"
  66.     exit( 0 )
  67.  
  68. # Intenta leer la imágen
  69. img = None
  70. try:
  71.     img = Image.open( sys.argv[ 1 ] )
  72. except:
  73.     print >>sys.stderr, "Error leyendo", sys.argv[ 1 ]
  74.     exit( 1 )
  75.  
  76. dim = img.size
  77.  
  78. # Lee el archivo a introducir
  79. f = None
  80. try:
  81.     f = open( sys.argv[ 2 ], "wb" )
  82. except:
  83.     print >>sys.stderr, "Error leyendo", sys.argv[ 2 ]
  84.     exit( 1 )
  85.  
  86. # Carga todo en memoria ( más rápido para cambiar muchos pixels )
  87. fst_img = img.load( )
  88.  
  89. inp = [] # Bits de entrada
  90.  
  91. bpp = len( img.getbands( ) ) # Bytes por pixel
  92.  
  93. # Mientras quede mensaje
  94. for y in xrange( dim[ 1 ] ):
  95.     for x in xrange( dim[ 0 ] ):
  96.         # Se lee la siguiente tupla
  97.         nxt_pix = tuple2arr( fst_img[ x, y ] )
  98.  
  99.         # Para cada byte
  100.         for i in xrange( bpp ):
  101.  
  102.             # se extrae el LSB
  103.             inp.append( extractLSB( nxt_pix[ i ] ) )
  104.  
  105.  
  106.         # Si hay para un byte
  107.         while len( inp ) > 8:
  108.             # Se juntan los bits y se escriben
  109.             f.write( composeByte( inp[ : 8 ] ) )
  110.             inp = inp[ 8 : ]
  111.  
  112. # Mientras quede para un byte
  113. while len( inp ) > 8:
  114.     f.write( composeByte( inp[ : 8 ] ) )
  115.     inp = inp[ 8 : ]
  116.  
  117. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement