Reading Japanese text from images

I’ve been intensively studying Japanese recently1, and something I have to do quite a lot is look up Japanese words that I see in images. Particularly words that involve a lot of kanji characters.

Last evening I hacked together a program that allows me to select arbitrary Japanese from an image on my screen, performs optical character recognition (OCR) on it, and outputs it as regular Unicode text. This regular text can then be looked up in an online Japanese-English dictionary, such as the excellent

Let’s take the following sentence:

Japanese sentence with lots of kanji

The first word in that sentence already has 4 kanji characters, which would normally take a long time to look up individually.2

With my program, I can take a screenshot of this sentence as it appears on my screen and the program will immediately output the following text:

  • 痴話喧嘩は続中か

Then I can lookup the words as usual from’s Words search:

  • 痴話喧嘩 = lover’s quarrel
  • = ?
  • = inside; in (a space or building)

Character recognition still isn’t perfect, as demonstrated by the character marked in pink above, which wasn’t decoded correctly.

Nevertheless even having a partially correct character is useful, as I can still use’s Kanji search to break it down into its component radicals:

  • = + + +

And then use a few of the correct radicals in the Kanji by Radicals search to quickly find the correct character:

  • = + + + | + 米

And so the original word can now be looked up correctly:

  • = continuation

How it Works

The program operates by waiting for you to take a screenshot using Command-Shift-4 (on the Mac), observing the new screenshot appearing on the desktop, and feeding it to the nhocr tool, which reads the Japanese text inside the screenshot and prints it.

Source Code

#!/usr/bin/env python
# Waits for screenshots to appear on the desktop,
# scans them for Japanese text using OCR,
# and prints out the text that was decoded from the image.
# This script is useful for rapidly converting on-screen Japanese
# text to actual text that can be looked up.
# Prerequisites:
#   * nhocr
#   * ImageMagick
#   * Python 2.7 - maybe 2.6 okay too
#   * watchdog
# @author David Foster

import os.path
import subprocess
import time
from watchdog.observers import Observer
from import FileSystemEventHandler

class ScreenshotFileCreationEventHandler(FileSystemEventHandler):
    def on_created(self, event):
        if not event.is_directory:
            if 'Screen Shot' in os.path.basename(event.src_path):
                # Convert screenshot to PPM format with ImageMagick
                    'convert', event.src_path, '/tmp/line.ppm'])

                line = subprocess.check_output([
                    'nhocr', '-line', '/tmp/line.ppm', '-o', '-'])
                print line.decode('utf-8'),

event_handler = ScreenshotFileCreationEventHandler()

observer = Observer()
observer.schedule(event_handler, '/Users/me/Desktop', recursive=False)

print 'Waiting for screenshots on the desktop to analyze...'
    while True:
except KeyboardInterrupt:

  1. All this studying is one reason I haven’t posted in a while.

  2. Individual kanji can be looked up by their component radicals at’s Kanji by Radicals search. Unfortunately it can take a few minutes to lookup each character.