pic2html v0.2
#!/usr/bin/envpython##pic2htmlv0.2a##Asmallcgi-scripttoconvertbinary-picturesto#colorizedhtml-data.##Author:RikardBosnjakovicbos@hack.org,2001-03-27#updated2003-09-08#updated2004-08-01#importcgi,Image
,os,StringIO,sys,tempfile,urllibprint"Content-type:text/html\n"#maxsize(widthorheight,inpixels)MAXSIZE=200#readthefiletouseasthebackgroundandremoveallcrappycharstxt=open("source").read()foriin"\n":txt
=txt.replace(i,"")#gettheurltothepicturestorage=cgi.FieldStorage()ifnotstorage.has_key("url"):print"EmptyURL."sys.exit()url=storage["url"].valueifurllib.unquote(url).find("file://")-1:print"Notallowed
tousefile://."sys.exit()try:urlfile=urllib.urlopen(url)exceptIOError:print"Anerroroccuredwhileretrievingthefile."sys.exit()pagedata=urlfile.read()try:im=Image.open(StringIO.StringIO(pagedata))except:p
rint"ERROR-",sys.exc_type,":",sys.exc_valueprint#print"Ifthedebuginfoabovewastoocryptic,thentheauthorstillwouldliketoseeit!"sys.exit()##grayscalepicturesdoesn'tworkyet.#isthecoloraninteger(index)oratu
ple(palette)?iftype(im.getpixel((0,0)))==type(0):#palette.obtainitandputtheminalistof3-tuples(R,G,B)palette=im.getdata().getpalette()pal=[tuple(palette[3*i:3*i+3])foriinrange(len(palette)/3)]else:#the
paletteisstoredineachpixel,notindexedpal=()#rescalethepicturetobetterproportionalsx=im.size[0]y=im.size[1]ifx1000ory1000:print"Toobigpicture,tryasmallerone(below1000*1000)."sys.exit()ifxMAXSIZE:im=im.
resize((MAXSIZE,(MAXSIZE*y)/x))x=im.size[0]y=im.size[1]ifyMAXSIZE:im=im.resize(((MAXSIZE*x)/y,MAXSIZE))im=im.resize((im.size[0],im.size[1]/2))#printthepictureinasciiandsaveittempfile.tempdir="/home/bo
s/public_html/pic2html/tmp/"tmpname=tempfile.mktemp(".html")f=open(tmpname,"a")pagedata="htmlheadtitlepic2htmlv0.2lt;bos@hack.org"pagedata+="gt;/title/head"pagedata+='bodybgcolor="#000000"basefontsize
=1pre'pri