Advertisement
abdulfatirs

ImageEncoder.py | Encode text into images

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