Skip to content

Instantly share code, notes, and snippets.

@KillerGoldFisch
Last active December 17, 2015 22:49
Show Gist options
  • Save KillerGoldFisch/5685194 to your computer and use it in GitHub Desktop.
Save KillerGoldFisch/5685194 to your computer and use it in GitHub Desktop.
Convert an Image to text of colored HTML using Python
# -*- coding: utf-8 -*-
import os
import urllib2
from PIL import Image
from os.path import basename
from urlparse import urlsplit
cfill = r"█"
Chars = [
[ r"█" , r"▓" , r"▒" , r"░" , r" " ]
]
def url2name(url):
return basename(urlsplit(url)[2])
def download(url, destDir = "./", localFileName = None):
localName = url2name(url)
req = urllib2.Request(url)
r = urllib2.urlopen(req)
if r.info().has_key('Content-Disposition'):
# If the response has Content-Disposition, we take file name from it
localName = r.info()['Content-Disposition'].split('filename=')[1]
if localName[0] == '"' or localName[0] == "'":
localName = localName[1:-1].split("'")[0].split('"')[0]
elif r.url != url:
# if we were redirected, the real file name we take from the final URL
localName = url2name(r.url)
if localFileName:
# we can force to save the file as specified name
localName = localFileName
localName = os.path.abspath(os.path.join( destDir, localName))
f = open(localName, 'wb')
f.write(r.read())
f.close()
return localName
def getChar(pix ,charset):
chars = Chars[charset]
charindex = sum(pix) * len(chars) / ( 255 + 255 + 255) - 1
charindex = charindex if charindex >= 0 else 0
return chars[charindex]
def getColorTag(pix):
return "".join(['0'*(2-len(x)) + x for x in [hex(y)[2:] for y in pix]])
class ImgFile:
def __init__(self, path):
self.tempFile = False
if path.lower().startswith("http://"):
self.tempFile = True
import tempfile
self.imgPath = download(path, tempfile.gettempdir())
else:
self.tempFile = False
self.imgPath = path
self.image = Image.open(self.imgPath)
self.image = self.image.convert('RGB')
self.width, self.height = self.image.size
def __del__(self):
if self.tempFile:
import os
os.unlink(self.imgPath)
def resize(self, w = -1, h = -1):
w = w if w > 0 else self.width
h = h if h > 0 else self.height
self.image.thumbnail((w, h), Image.ANTIALIAS)
self.width, self.height = self.image.size
def getPixels(self):
return (self.width, self.height, self.image.load())
def toTEXT(self):
w, h, pixels = self.getPixels()
txtout = ""
for y in range(h):
for x in range(w):
txtout += getChar(pixels[x,y], 0)
txtout += "\n"
return txtout
def toHTML(self):
w, h, pixels = self.getPixels()
txtout = '''<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<div class="fontbox" style="font-family:monospace; font-size:8px; line-height: 100%">
'''
for y in range(h):
currenttag = ""
for x in range(w):
tag = getColorTag(pixels[x,y])
if tag != currenttag:
if currenttag != "":
txtout += "</font>"
txtout += '<font color="#' + tag + '">'
currenttag = tag
txtout += cfill
txtout += "</font><br/>\n"
return txtout
if __name__ == '__main__':
import argparse
import sys
parser = argparse.ArgumentParser(description='Process images to Text or HTML.')
parser.add_argument("-i", type=str, help="Image file path or URL")
parser.add_argument("-x", type=int, default=-1, help="Width of Image")
parser.add_argument("-y", type=int, default=-1, help="Height of Image")
parser.add_argument("-t", type=str, default="text", help="output Type [text|html]")
parser.add_argument("-d", type=bool, default=False, help="Debug [True|False]")
argv = sys.argv
argvres = parser.parse_args(sys.argv[1:])
if argvres.d:
print "-i : %s\n-x : %i\n-y : %i\n-t : %s"%(argvres.i, argvres.x, argvres.y, argvres.t)
img = ImgFile(argvres.i)
resizeParams = dict()
if argvres.x > 0:
resizeParams["w"] = argvres.x
if argvres.y > 0:
resizeParams["h"] = argvres.y
if len(resizeParams) > 0:
img.resize(**resizeParams)
if argvres.t == "text":
print img.toTEXT()
elif argvres.t == "html":
print img.toHTML()
else:
print "Unknown Type: " +argvres.t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment