#!/usr/bin/env python ## image2text V0.1 # # This program takes an image and alot of text and htmlizes the text to be # the image... Will not work unless image is RGB (and I haven't fixed it to # work well with all images... ) # # REQUIRES: Python Imaging Library # # Copyleft (2004) Andrew Gwozdziewycz # # This program is free software; you can redistribute and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or visit # the Free Software Foundation website at http://www.fsf.org image = None output = None text = None def image2text(fileimage, textinput, outfile): import Image curcolor = '#000000' oldcolor = '#000000' img = Image.open(fileimage) xsize, ysize = img.size pixels = list(img.getdata()) pixelnumber = 0 textindex = 0 outfile.write('
\n' % curcolor)
    for cury in range(ysize):
        for curx in range(xsize):
            ch = textinput[textindex]
            while ch == '\r' or ch == '\n' or ch == ' ' or ch == '\t':
                textindex = textindex + 1
                ch = textinput[textindex]
            ch = ch.replace('&', '&')
            ch = ch.replace('<', '<')
            ch = ch.replace('>', '>')
            curcolor = '#%.2x%.2x%.2x' % pixels[pixelnumber]

            if curcolor != oldcolor:
                outfile.write('%s' % (curcolor, ch))
            else:
                outfile.write( ch)
            pixelnumber = pixelnumber + 1
            textindex = textindex + 1           
        outfile.write('\n')
    outfile.write("")
    outfile.write("
\n") def _do(): import sys, os global image, text, output inchars = None if text == None: inchars = sys.stdin.read() else: if os.path.exists(text): try: inp = open(text, 'r') inchars = inp.read() except IOError: sys.stderr.write('image2text: couldn\'t read text\n') return if image == None or not os.path.exists(image): if image == None: sys.stderr.write('image = none') sys.stderr.write(\ 'image2text: image does not exist or was not given\n') return try: outstream = None if output != None: outstream = open(output, 'w') else: outstream = sys.stdout image2text(image, inchars, outstream) except IOError: sys.stderr.write('image2text: couldn\'t output text\n') def usage(): import sys sys.stderr.write('image2text \n') sys.stderr.write(' -h, --help - this message\n') sys.stderr.write(' -i, --image - image file\n') sys.stderr.write(' -o, --output - output file\n') sys.stderr.write(' -t, --text - textfile to image\n') def main(): import getopt, sys global image, text, output try: opts, args = getopt.getopt(sys.argv[1:], 'hi:t:o:', ['help', 'image=', 'text=', 'output=']) except getopt.GetoptError: usage() sys.exit(2) for o, a in opts: if o in ('-h', '--help'): usage() sys.exit() if o in ('-o', '--output'): output = a print output, '=', a if o in ('-i', '--image'): image = a print 'image =', a if o in ('-t', '--text'): text = a print 'text =', a _do() if __name__ == '__main__': main()