#!/usr/bin/python # -*- coding: utf-8 -*- # pyimg2html v0.0.0.1 # code by shellex import Image import sys import random adjust = 0.7 char_set = 'A' font_size = 3 char_set_len = len(char_set) def make_html_tag(rgb): color = '' for e in rgb: c = hex(e).replace('0x', '') color += '0'+c if len(c) == 1 else c ch = char_set[int(random.random()*char_set_len)] tag = '%s' % (font_size,color, ch) return tag def make_matrix(infile, outfile, new_w, new_h, zoom): im = Image.open(infile) im = im.convert('RGB') w, h = im.size im = im.resize((w, int(h*adjust))) w, h = im.size if new_w!=-1 and new_h!=-1: im = im.resize((new_w, new_h)) w, h = im.size if zoom != -1: im = im.resize((int(w*zoom), int(h*zoom))) w, h = im.size pix = im.load() f = open(outfile, 'w') f.write('

'%font_size) for y in range(0, h): for x in range(0, w): f.write(make_html_tag(pix[x, y])) f.write('
\n') f.write('

') f.close() if __name__ == "__main__": if len(sys.argv) == 1: print 'usage:\n\t%s [[new width] [new height] | [zoom]]' % sys.argv[0] sys.exit(0) w, h, zoom = -1, -1, -1 file_name = sys.argv[1] if len(sys.argv) == 3: zoom = float(sys.argv[2]) elif len(sys.argv) >= 4: w, h = sys.argv[2], sys.argv[3] else: pass make_matrix(file_name, file_name + '.html', w, h, zoom )