20100513

source code for converting b&w images to pure HTML

imagefake.py



   1:   

   2:  import sys

   3:  import re

   4:  import os

   5:   

   6:  '''

   7:  open html file

   8:  find all image tag

   9:  for each image tag

  10:  remove it

  11:  strip out the image name

  12:  find that image

  13:  convert it to HTML

  14:  replace the image tag with this html

  15:  '''

  16:   

  17:  infilename  = sys.argv[1]

  18:  outfilename = sys.argv[2]

  19:  infile  = open( infilename,"r")

  20:  outfile = open(outfilename,"w")

  21:   

  22:  document = "".join(infile.readlines())

  23:   

  24:  image_tags = re.findall(r'<IMG [^>]*>',document)

  25:   

  26:  print image_tags

  27:   

  28:  urls = map(lambda s:s.split('SRC=')[1].split('"')[1],image_tags)

  29:   

  30:  os.system("javac *.java")

  31:   

  32:  for tag in image_tags:

  33:      url = (lambda s:s.split('SRC=')[1].split('"')[1])(tag)

  34:      os.system("rm temp.html.stub; java imagehack %s temp.html"%url)

  35:      print "reading temp.html"

  36:      try:

  37:          f   = open("temp.html.stub",'r')

  38:          obj = "".join(f.readlines())

  39:          f.close()

  40:          document = re.sub(tag,obj,document)

  41:      except IOError:

  42:          pass 

  43:   

  44:  try:

  45:      f   = open("temp.html.headers",'r')

  46:      headers = "".join(f.readlines())

  47:      f.close()

  48:      document = re.sub(r'</HEAD>',headers,document)

  49:  except IOError:

  50:      pass 

  51:          

  52:  outfile.write(document)

  53:   

  54:   

  55:  infile .close()

  56:  outfile.close()

  57:   




imagehack.java



   1:  import java.awt.image.BufferedImage;

   2:  import java.io.BufferedOutputStream;

   3:  import java.io.File;

   4:  import java.io.FileOutputStream;

   5:  import java.io.IOException;

   6:  import java.io.PrintWriter;

   7:  import java.util.HashSet;

   8:  import java.util.Set;

   9:  import javax.imageio.ImageIO;

  10:  import java.net.URL;

  11:  import static java.lang.System.*;

  12:  public class imagehack {

  13:      static int depth=8;

  14:      static int D=depth-1;

  15:      static class block {

  16:          public int x,y,w,h,c;

  17:          public block(int a,int b,int f, int d, int e) {x=a;y=b;w=f;h=d;c=e;}

  18:          public boolean equals(block b) {return x==b.x&&y==b.y&&w==b.w&&h==b.h&&c==b.c;}}

  19:      static int foo(int x) { return x/(256/depth); }

  20:      static String tohex(int x) { 

  21:          x*=(256/depth); 

  22:          return ""+("0123456789ABCDEF".charAt((x>>4)&0xf))+("0123456789ABCDEF".charAt(x&0xf)); }

  23:      public static void main(String[] args) throws IOException {

  24:          String filename = args[0];

  25:          String outputname = args[1];

  26:          out.println("input "+filename);

  27:          out.println("output "+outputname);

  28:          BufferedImage hold;

  29:          try {

  30:              hold = ImageIO.read(new File(filename));

  31:          } catch (Exception e) {

  32:              hold = ImageIO.read(new URL(filename));

  33:          }

  34:          int w = hold.getWidth();

  35:          int h = hold.getHeight();

  36:          Set<block> blocks = new HashSet<block>();

  37:          int[][] data = new int[h][w];

  38:          for (int i=0;i<h;i++) {

  39:              int linestart = -1; int linecolor = D;

  40:              for (int j=0;j<w;j++) {

  41:                  int c=foo(hold.getRGB(j,i)&0xff);

  42:                  if (c!=linecolor) {

  43:                      if (j>0 && linecolor<D)

  44:                          blocks.add(new block(i,linestart,j-linestart,1,linecolor));

  45:                      linecolor=c; linestart=j; } }

  46:              if (linecolor<D)

  47:                  blocks.add(new block(i,linestart,w-linestart,1,linecolor)); }

  48:          out.println("initially "+blocks.size()+" blocks");

  49:          if (blocks.size()>1000) return;

  50:          String wrap =

  51:          "#dumbwrap1 {\n"+

  52:          "  margin:0px;\n"+

  53:          "  padding:0px;\n"+

  54:          "  border:0px none;\n"+

  55:          "  background:#fff;\n"+

  56:          "  position:static;\n"+

  57:          "  display:inline-block;\n"+

  58:          "}";

  59:          String images =

  60:          "#imgwrap2 {\n"+

  61:          "  position:relative;\n"+

  62:          "}";

  63:          File ofile = new File(outputname);

  64:          PrintWriter p = new PrintWriter(new BufferedOutputStream(new FileOutputStream(ofile)));

  65:          p.println("<head>");

  66:          String css = "";

  67:          css += "<style type=\"text/css\">\n";

  68:          for (int i=0; i<D; i++) {

  69:              String c = tohex(i);

  70:              css += 

  71:              "#bb"+c+" {\n"+

  72:              "  margin:0px;\n"+

  73:              "  padding:0px;\n"+

  74:              "  border:0px none;\n"+

  75:              "  background:#"+c+c+c+";\n"+

  76:              "  position:absolute;\n"+

  77:              "}\n"; }

  78:          css += wrap;

  79:          css += images;

  80:          css += "</style>";

  81:          p.println(css);

  82:          p.println("</head>");

  83:          p.println("<body>");

  84:          String imagecode = "";

  85:          imagecode += "<div id='dumbwrap1' style='width:"+w+"px;height:"+h+"px;'>";

  86:          imagecode += "<div id='imgwrap2'>";

  87:          for (block b : blocks)

  88:              imagecode += "<div id='bb"+tohex(b.c)+

  89:              "' style='top:"+b.x+"px;left:"+b.y+"px;width:"+b.w+

  90:              "px;height:"+b.h+"px;'></div>";

  91:          imagecode +="</div></div>";

  92:          p.print(imagecode);

  93:          p.println("</body>");

  94:          p.flush();p.close();

  95:          

  96:          //print just the image if external header is used

  97:          ofile = new File(outputname);

  98:          p = new PrintWriter(new BufferedOutputStream(new FileOutputStream(ofile+".stub")));

  99:          p.print(imagecode);

 100:          p.flush();p.close();

 101:          

 102:          ofile = new File(outputname);

 103:          p = new PrintWriter(new BufferedOutputStream(new FileOutputStream(ofile+".headers")));

 104:          p.println(css);

 105:          p.flush();p.close();

 106:      }

 107:  }


1 comment: