<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>x + 3 &#187; Python</title>
	<atom:link href="http://xplus3.net/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://xplus3.net</link>
	<description></description>
	<lastBuildDate>Fri, 19 Aug 2011 01:05:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Convert hOCR to PDF</title>
		<link>http://xplus3.net/2009/04/02/convert-hocr-to-pdf/</link>
		<comments>http://xplus3.net/2009/04/02/convert-hocr-to-pdf/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 20:05:46 +0000</pubDate>
		<dc:creator>Jonathan Brinley</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[hOCR]]></category>
		<category><![CDATA[HocrConverter]]></category>
		<category><![CDATA[OCR]]></category>
		<category><![CDATA[OCRopus]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://xplus3.net/?p=207</guid>
		<description><![CDATA[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&#8217; class and title attributes to convey information from the OCR process, as you can see from this &#8230; <a href="http://xplus3.net/2009/04/02/convert-hocr-to-pdf/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As <a href="http://xplus3.net/2009/03/31/ocr-with-ocropus-and-tesseract/">I mentioned recently</a>, 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 <a href="http://en.wikipedia.org/wiki/HOCR_(software)"><abbr title="Hebrew Optical Character Recoginition">HOCR</abbr></a>). It is basically a microformat using <code>div</code> and <code>span</code> tags&#8217; <code>class</code> and <code>title</code> attributes to convey information from the OCR process, as you can see from this example of a basic hOCR document:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="xml"><pre class="de1"><span class="sc0">&lt;!DOCTYPE html</span>
<span class="sc0">  PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span>
<span class="sc0">  &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span class="sc3"><span class="re1">&lt;html</span> <span class="re0">xmlns</span>=<span class="st0">&quot;http://www.w3.org/1999/xhtml&quot;</span><span class="re2">&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;head<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;meta</span> <span class="re0">content</span>=<span class="st0">&quot;ocr_line ocr_page&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;ocr-capabilities&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;meta</span> <span class="re0">content</span>=<span class="st0">&quot;en&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;ocr-langs&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;meta</span> <span class="re0">content</span>=<span class="st0">&quot;Latn&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;ocr-scripts&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;meta</span> <span class="re0">content</span>=<span class="st0">&quot;&quot;</span> <span class="re0">name</span>=<span class="st0">&quot;ocr-microformats&quot;</span><span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;title<span class="re2">&gt;</span></span></span>OCR Output<span class="sc3"><span class="re1">&lt;/title<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/head<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;body<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;div</span> <span class="re0">class</span>=<span class="st0">&quot;ocr_page&quot;</span> <span class="re0">title</span>=<span class="st0">&quot;bbox 0 0 2548 3300; image /path/to/scanned/image.png&quot;</span><span class="re2">&gt;</span></span>
      <span class="sc3"><span class="re1">&lt;span</span> <span class="re0">class</span>=<span class="st0">&quot;ocr_line&quot;</span> <span class="re0">title</span>=<span class="st0">&quot;bbox 659 143 863 177&quot;</span><span class="re2">&gt;</span></span>Some Text<span class="sc3"><span class="re1">&lt;/span<span class="re2">&gt;</span></span></span>
      <span class="sc3"><span class="re1">&lt;span</span> <span class="re0">class</span>=<span class="st0">&quot;ocr_line&quot;</span> <span class="re0">title</span>=<span class="st0">&quot;bbox 723 275 916 324&quot;</span><span class="re2">&gt;</span></span>More Text<span class="sc3"><span class="re1">&lt;/span<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/div<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/body<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/html<span class="re2">&gt;</span></span></span></pre></div></div></div></div></div></div></div>


<p>For the more details, see <a href="http://docs.google.com/View?docid=dfxcv4vc_67g844kf">Thomas Breuel&#8217;s complete hOCR specification draft</a>. The example, though, shows all that I need to know. From the <code>div[@class='ocr_page']</code>, 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 <code>span[@class='ocr_line']</code>, 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.</p>
<p>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.<br />
<span id="more-207"></span></p>
<h3>Creating a PDF</h3>
<p>A while back, Florian Hackenberger  created a <a href="http://groups.google.com/group/ocropus/browse_thread/thread/3cf464bda5807952">basic hOCR to PDF converter in Java</a>. It does its job reasonably well, but it&#8217;s somewhat rough around the edges. Since I&#8217;m more familiar with Python than Java, it seemed like a good idea to rewrite the code in Python, so I could more comfortably hack it.</p>
<p>And so I present to you, dear reader, the <a href="http://github.com/jbrinley/HocrConverter">hOCR Converter Python script</a> (note: this script depends on the <a href="http://www.reportlab.org/downloads.html#reportlab">ReportLab PDF Library</a>, which, in turn, depends on the <a href="http://www.freetype.org/">FreeType 2 Font Engine</a>). Right now, it has two primary functions:</p>
<ol>
<li>Given an hOCR file, create a text-only (<em>i.e.</em>, no HTML) document</li>
<li>Given an hOCR file and an image, create a PDF</li>
</ol>
<p>Usage is pretty simple:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="python"><pre class="de1"><span class="kw1">from</span> HocrConverter <span class="kw1">import</span> HocrConverter
hocr <span class="sy0">=</span> HocrConverter<span class="br0">&#40;</span><span class="st0">&quot;myHocrFile.html&quot;</span><span class="br0">&#41;</span>
hocr.<span class="me1">to_text</span><span class="br0">&#40;</span><span class="st0">&quot;output.txt&quot;</span><span class="br0">&#41;</span>
hocr.<span class="me1">to_pdf</span><span class="br0">&#40;</span><span class="st0">&quot;myImageFile.png&quot;</span><span class="sy0">,</span> <span class="st0">&quot;output.pdf&quot;</span><span class="br0">&#41;</span></pre></div></div></div></div></div></div></div>


<p>You end up with a text file at <code>output.txt</code> containing the contents of the <code>body</code> of the hOCR document and a PDF file at <code>output.pdf</code> containing your image superimposed on top of the text.</p>
<p>Note that the image you use to create the PDF need not be the same image used for the OCR. If, for example, you used a 300 or 400 DPI image for the OCR, but you want a smaller file for the PDF, you can create a 72 DPI version of the image and feed that through the script instead of the original.</p>
<p>With a simple script that iterates through a directory of images, calling OCRopus and this script for each image (maybe with some ImageMagick and <a href="http://xplus3.net/2008/10/09/command-line-pdf-editing/">pdftk</a> thrown in for good measure), you can quickly OCR a large batch of images and convert them to searchable PDF documents.</p>
<h3>Future Development</h3>
<p>I&#8217;d like to add the capability to convert the output of various OCR programs into hOCR format. For example, OmniPage, which is much better than OCRopus when it comes to layout analysis for complex images, can output documents in its proprietary XML schema. I should be able to transform that into an hOCR document and then use this script to create a PDF from there.</p>
<p>I would welcome any other suggestions or contributions.</p>
<p><strong><a href="http://github.com/jbrinley/HocrConverter">Download the HocrConverter script from github</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://xplus3.net/2009/04/02/convert-hocr-to-pdf/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>How to Install rdflib</title>
		<link>http://xplus3.net/2009/03/09/how-to-install-rdflib/</link>
		<comments>http://xplus3.net/2009/03/09/how-to-install-rdflib/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 17:08:27 +0000</pubDate>
		<dc:creator>Jonathan Brinley</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[rdflib]]></category>

		<guid isPermaLink="false">http://xplus3.net/?p=188</guid>
		<description><![CDATA[At the Linked Data preconference at Code4Lib 2009 a couple of weeks ago, I learned about the rdflib Python library. Naturally, I wanted to install the library so I could mess with it on my own. That proved to be a little problematic, though. Following the installation instructions, I typed: $ easy_install -U "rdflib&#62;=2.4,&#60;=3.0a" And it very helpfully told me &#8230; <a href="http://xplus3.net/2009/03/09/how-to-install-rdflib/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>At the Linked Data preconference at <a href="http://code4lib.org/conference/2009/">Code4Lib 2009</a> a couple of weeks ago, I learned about the <a href="http://www.rdflib.net/">rdflib Python library</a>. Naturally, I wanted to install the library so I could mess with it on my own. That proved to be a little problematic, though. Following the installation instructions, I typed:</p>
<pre>$ easy_install -U "rdflib&gt;=2.4,&lt;=3.0a"</pre>
<p>And it very helpfully told me (among other things):</p>
<pre>gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O2 -Wall -Wstrict-prototypes
-fPIC -I/usr/include/python2.5 -c src/bison/SPARQLParser.c -o
build/temp.linux-i686-2.5/src/bison/SPARQLParser.o
src/bison/SPARQLParser.c:7:20: error: Python.h: No such file or directory</pre>
<p>As it turns out, I should have had <code>python-dev</code> and <code>build-essential</code> already installed. Being new to Linux, I did not know. Thanks, <a href="http://oxfordrepo.blogspot.com/">BenO</a>, for helping me out with this.</p>
<p>The complete installation instructions:</p>
<pre>$ apt-get install python-dev
$ apt-get install build-essential
$ easy_install "rdflib==2.4.0"</pre>
]]></content:encoded>
			<wfw:commentRss>http://xplus3.net/2009/03/09/how-to-install-rdflib/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

