#!/usr/bin/env python """ ImageOut.py, Written by Alex Elder, of http://alexelder.tumblr.com Contact: alexelder [[at]] gmail [[dot] com ############################################################################# # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, 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, see . # ############################################################################# """ import getopt import sys import Image, ImageFont, ImageDraw def main(args): """ Command line flags: h, --help = display this help i = input image location [REQUIRED] t = input text file [REQUIRED] f = output font [REQUIRED] o = output filename (default: imgout.jpg) s = output font size (default: 10px) Example invocation: >python imgout.py -i my_input_file -t input_textfile.txt -f true_typefont.ttf -s 10 -o my_output.jpg """ try: opts, args = getopt.getopt(args, "i:t:o:s:f:h", ['help']) except getopt.GetoptError, err: print str(err) show_usage() sys.exit(2) input_image = None input_text = None font_size = None output_font = None output_image = None for opt, argument in opts: if opt in ("-h", "--help"): show_usage() sys.exit(2) elif opt == '-i': #input image file if argument == False: print "Input file argument required." sys.exit(2) input_image = argument elif opt == '-t': #input text file if argument == False: print "Input text file argument required." sys.exit(2) input_text = argument elif opt == '-o': #output image filename if argument == False: print "Output .jpg filename required." sys.exit(2) output_image = argument elif opt == '-s': #font size (denotes image size) if argument == False: font_size = 10 else: if argument < 2: font_size = 2 else: try: font_size = int(argument) except ValueError, err: print "Invalid number sequence entered: %d." % argument sys.exit(2) elif opt == '-f': # Location of a .ttf file if argument == False: print "You must specify a .ttf font. Try checking your %FONTS% folder." sys.exit(2) else: output_font = argument if input_image is None: print "You MUST supply an input image. Run --help for details." sys.exit(2) if input_text is None: print "You MUST supply an input text file. Run --help for details." sys.exit(2) if output_font is None: print "You MUST supply an output font for use. Run --help for details" sys.exit(2) if font_size is None: font_size = 10 # Read and prepare the text file try: chars = open(str(input_text), 'r').read() except IOError, err: print "The text file `%s' doesn't exist." % input_text sys.exit(2) chars = chars[::-1] chars = chars.replace('.','').replace(',','').replace('\'','').replace(' ','')\ .replace(';','').replace(':','').replace('\n','').replace('\r','') chars = list(chars) # Open the input image try: input_im = Image.open(input_image) except IOError, error: print "The image `%s' could not be found." sys.exit(2) # Create the output image memory space # Remarks: # The output size of the image is related to the size of the font (even numbers work best). # The default background colour is black. Messing with this normally makes images look a /bit/ weird. output_im = Image.new("RGB", (input_im.size[0]*(font_size/2),input_im.size[1]*(font_size/2)),color=(0,0,0)) output_dr = ImageDraw.Draw(output_im) try: font = ImageFont.truetype(output_font, font_size) except IOError, error: print "Unknown font `%s'. Please enter the fully qualified path to your font if it isn't in THIS directory." sys.exit(2) # Some horrible hack to make sure that we *always* have more input characters than pixels while ((input_im.size[0]*input_im.size[1]) > len(chars)): chars += chars # Loop over the image, rows, then columns (legacy code from the HTML exporter) for y in range(0, input_im.size[1]): for x in range(0, input_im.size[0]): pix = input_im.getpixel((x,y)) letter = chars.pop() output_dr.text((x*(font_size/2),y*(font_size/2)), letter, fill=pix, font=font) output_im.save(output_image, "JPEG") del output_im del input_im # The Zen of Python, Aphroism 12: (Tim Peters) # ``In the face of ambiguity, refuse the temptation to guess.'' def show_usage(): print """ Command line flags: h, --help = display this help i = input image location [REQUIRED] t = input text file [REQUIRED] f = output font [REQUIRED] o = output filename (default: imgout.jpg) s = output font size (default: 10px) """ if __name__ == "__main__": main(sys.argv[1:])