Tuesday, 3 January 2012

Convert Images to text and text back to images

When we convert images to/from text we use base64 and import python you can use base64 by 
import base64
 So this script will open the image from the first argument and save to readable text version as output.txt


import sys, base64

img = open(sys.argv[1], 'r')
txt = img.read()
txt2 = base64.b64encode(txt)
txtfile = open("output.txt", 'w')
txtfile.write(txt2)
#print txt2

If you uncomment the last line by removing the '#' it will print out the text value of the image.

And this 

import sys, base64

img = open(sys.argv[1], 'r')
txt = img.read()
txt2 = base64.b64decode(txt)
txtfile = open("output.png", 'w')
txtfile.write(txt2)
Will open the text file as the first argument and output the image to 'output.png'

Download

No comments:

Post a Comment