Monthly Archives: April 2009

Naming a .htaccess File in Windows

Windows likes files to have names. If you tell windows to change the name of a file to “.htaccess“, it will complain: “You must type a file name.” It seems to think that “htaccess” is the file extension for a nameless file.

I just stumbled across a workaround, though. Name a file “.htaccess.” (notice the extra dot at the end). Windows (Vista, not XP) will remove the trailing dot and give you a file named “.htaccess“.

Subdomains for localhost

A handy trick I just learned: you can use your hosts file to create subdomains of localhost.

For example, I have WordPress and Drupal both running on my machine for local web development testing. I used to access one at http://localhost/wp and the other at http://localhost/drupal. This works just fine, in general, but can lead to some awkwardness with things like .htaccess files and relative links when I move the site I’m developing to a real server.

By editing the hosts file, though, I can access my development sites at http://wp.localhost/ and http://drupal.localhost/, making it a little easier to test, migrate, etc.

In Windows (XP and Vista), you can find your hosts file in the C:\Windows\System32\drivers\etc directory. Add a couple of lines to the end of the file:

127.0.0.1       wp.localhost
127.0.0.1       drupal.localhost

And create virtual hosts in your Apache httpd.conf to point those domains to the correct directories. E.g.:

<VirtualHost *:80>
    DocumentRoot D:\xampp\htdocs\drupal
    ServerName drupal.localhost
</VirtualHost>

Convert hOCR to PDF

As I mentioned recently, OCRopus OCR software output an hOCR file. What is hOCR? hOCR is an open standard for representing OCR results in an HTML document (not to be confused with HOCR). It is basically a microformat using div and span tags’ class and title attributes to convey information from the OCR process, as you can see from this example of a basic hOCR document:

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta content="ocr_line ocr_page" name="ocr-capabilities"/>
    <meta content="en" name="ocr-langs"/>
    <meta content="Latn" name="ocr-scripts"/>
    <meta content="" name="ocr-microformats"/>
    <title>OCR Output</title>
  </head>
  <body>
    <div class="ocr_page" title="bbox 0 0 2548 3300; image /path/to/scanned/image.png">
      <span class="ocr_line" title="bbox 659 143 863 177">Some Text</span>
      <span class="ocr_line" title="bbox 723 275 916 324">More Text</span>
    </div>
  </body>
</html>

For the more details, see Thomas Breuel’s complete hOCR specification draft. The example, though, shows all that I need to know. From the div[@class='ocr_page'], I can learn the dimensions (in pixels) of the image that was OCRed (as well as the path to that image on my machine). From a span[@class='ocr_line'], I can learn the location and dimensions of the bounding box around a line of recognized text, as well as the content of that line.

That information, along with a copy of the original image (or a sufficiently similar image) is enough to create a PDF of the image with selectable text.
Continue reading

Find Files by Size

Find all TIFFs in a directory smaller than 90 MB:

$ find /dir/to/search -name *.tif -size -90M -exec ls -lh {} \;

Get just the size and path and write to a file:

$ find /dir/to/search -name *.tif -size -90M -exec ls -lh {} \; | awk '{print $5 , $8}' > output.txt

Useful for finding images that might have been scanned at the wrong resolution/bit depth/etc.