abdulfatirs

ImageEncoder.py | Encode text into images

Mar 14th, 2014
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # ImageEncoder.py
  3. # Author: Abdul Fatir
  4.  
  5. from PIL import Image
  6. import optparse
  7.  
  8. morse_list={
  9. 'A':'.-','B':'-...','C':'-.-.','D':'-..','E':'.','F':'..-.',
  10. 'G':'--.','H':'....','I':'..','J':'.---','K':'-.-','L':'.-..',
  11. 'M':'--','N':'-.','O':'---','P':'.--.','Q':'--.-','R':'.-.',
  12. 'S':'...','T':'-','U':'..-','V':'...-','W':'.--','X':'-..-',
  13. 'Y':'-.--','Z':'--..','0':'-----','1':'.----','2':'..---','3':'...--',
  14. '4':'....-','5':'.....','6':'-....','7':'--...','8':'---..','9':'----.',
  15. '.':'.-.-.-',',':'--..--','?':'..--..',"'":'.----.','/':'-..-.','(':'-.--.-',
  16. ')':'-.--.-',':':'---...',';':'-.-.-.','=':'-...-','+':'.-.-.','-':'-....-',
  17. '_':'..--.-','"':'.-..-.','$':'...-..-','':''
  18. }
  19.  
  20. def EncodeToImage(data,_PNG):
  21.     letters = list(data)
  22.     pixsum = 0
  23.     whitePixels = []
  24.     for letter in letters:
  25.         _lettermorse = morse_list[letter]
  26.         _morsechars = list(_lettermorse)
  27.         for morsechar in _morsechars:
  28.             intval = ord(morsechar)
  29.             pixsum += intval
  30.             whitePixels.append(pixsum)
  31.         pixsum += 32
  32.         whitePixels.append(pixsum)
  33.     W = 100
  34.     H = whitePixels[len(whitePixels)-1]/100 + 1
  35.     img = Image.new( 'RGB', (W,H), "black")
  36.     pixels = img.load()
  37.    
  38.     for whitePixel in whitePixels:
  39.         y = whitePixel/100
  40.         x = whitePixel%100
  41.         pixels[x,y] = (255,255,255)
  42.     img.save(_PNG)
  43.  
  44. def Main():
  45.     parser = optparse.OptionParser('usage: python ImageEncoder.py -o <output PNG image name>')
  46.     parser.add_option('-o',dest='outPNG',type='string',help='Please specify the output image file')
  47.     (options,arg) = parser.parse_args()
  48.     if (options.outPNG == None):
  49.         print parser.usage
  50.         exit(0)
  51.     else:
  52.         outPNG = options.outPNG
  53.     datatoencode = raw_input("Please enter data to encode (without spaces):")
  54.     EncodeToImage(datatoencode.upper(),outPNG)
  55.    
  56. if __name__ == '__main__':
  57.     Main()
Advertisement
Add Comment
Please, Sign In to add comment