Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 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. # Cambiar el LSB
  36. def changeLSB( byt, bt ):
  37.     if ( bt & 1 ) > 0:
  38.         byt |= 1
  39.     else:
  40.         byt &= 0xFE
  41.  
  42.     return byt
  43.  
  44. # Descompone un byte en array de bits
  45. def decomposeByte( bt ):
  46.    
  47.     out = []
  48.     i = 7
  49.     nbt = ord( bt )
  50.  
  51.     while i >= 0 :
  52.         out.append( nbt / ( 2**i ) )
  53.         nbt = nbt % ( 2**i )
  54.         i -= 1
  55.  
  56.     return out            
  57.  
  58. # Código en si
  59. import sys, os
  60. try:
  61.     from PIL import Image
  62. except:
  63.     print >>sys.stderr, "Necesitas PIL"
  64.     print >>sys.stderr, "[ http://www.pythonware.com/products/pil/ ]"
  65.     exit( 0 )
  66.  
  67. # Comprueba los argumentos
  68. if ( len( sys.argv ) < 4 ):
  69.     print >>sys.stderr, sys.argv[ 0 ], "<imágen> <archivo> <salida>"
  70.     exit( 0 )
  71.  
  72. # Intenta leer la imágen
  73. img = None
  74. try:
  75.     img = Image.open( sys.argv[ 1 ] )
  76. except:
  77.     print >>sys.stderr, "Error leyendo", sys.argv[ 1 ]
  78.     exit( 1 )
  79.  
  80. # Lee el archivo a introducir
  81. f = None
  82. try:
  83.     f = open( sys.argv[ 2 ], "rb" )
  84. except:
  85.     print >>sys.stderr, "Error leyendo", sys.argv[ 2 ]
  86.     exit( 1 )
  87.  
  88. # Muestra el espacio disponible
  89. fsize = os.path.getsize( sys.argv[ 2 ] )
  90.  
  91. dim = img.size
  92.  
  93. avail_size = ( dim[ 0 ] * dim [ 1 ] * len( img.getbands( ) ) ) / 8
  94.  
  95. print "Tamaño del archivo:", fsize, "bytes"
  96. print "Imágen:"
  97. print dim[ 0 ], "x", dim[ 1 ]
  98. print avail_size , "bytes"
  99. print float( avail_size ) / ( 1 << 10 ), "Kb"
  100.  
  101. if ( avail_size < fsize  ):
  102.     print >>sys.stderr, "No hay espacio suficiente"
  103.     exit( 2 )
  104.  
  105.  
  106. # Carga todo en memoria ( más rápido para cambiar muchos pixels )
  107. fst_img = img.load( )
  108.  
  109. x = 0 # Posición X
  110. y = 0 # Posición Y
  111.  
  112. msg = f.read(1) # Byte actual
  113. inp = [] # Bits de entrada
  114.  
  115. bpp = len( img.getbands( ) ) # Bytes por pixel
  116.  
  117. # Mientras quede mensaje
  118. while ( len( msg ) > 0 ):
  119.  
  120.     # Se añade a la lista el mensaje
  121.     inp = inp + decomposeByte(msg[0])
  122.  
  123.     # Mientras queden datos de entrada
  124.     while ( len ( inp ) > 0 ):
  125.  
  126.         # Si no hay suficientes para un pixel, se leen
  127.         if ( len ( inp ) < bpp ):
  128.             msg = f.read( 1 )
  129.  
  130.             if len( msg ) < 1 :
  131.                 break
  132.  
  133.             inp = inp + decomposeByte( msg[ 0 ] )
  134.  
  135.         # Se lee la siguiente tupla
  136.         nxt_pix = tuple2arr( fst_img[ x, y ] )
  137.  
  138.         # Para cada byte
  139.         for i in xrange( bpp ):
  140.  
  141.             # se cambia el LSB
  142.             nxt_pix[ i ] = changeLSB( nxt_pix[ i ], inp.pop( 0 ) )
  143.  
  144.         # Se guarda la tupla
  145.         fst_img[ x, y ] = arr2tuple( nxt_pix )
  146.  
  147.         # Y se pasa al siguiente pixel
  148.         x += 1
  149.         if ( x >= dim[ 0 ] ):
  150.             x = 0
  151.             y += 1
  152.  
  153.     msg = f.read( 1 )
  154.  
  155. # Si aún queda algo
  156. if ( len( inp ) > 0 ):
  157.     # Se lee la siguiente tupla
  158.     nxt_pix = tuple2arr( fst_img[ x, y ] )
  159.  
  160.     # Para cada byte
  161.     for i in xrange( bpp ):
  162.         if ( len( inp ) < 1 ):
  163.             break
  164.  
  165.         # se cambia el LSB
  166.         nxt_pix[ i ] = changeLSB( nxt_pix[ i ], inp.pop( 0 ) )
  167.  
  168.     # Se guarda la tupla
  169.     fst_img[ x, y ] = arr2tuple( nxt_pix )
  170.  
  171. # Ya se acabo con el archivo    
  172. f.close()
  173.  
  174. # Por último se guarda en la imágen
  175. img.save( sys.argv[ 3 ] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement