DEV Community

george_pollock
george_pollock

Posted on • Originally published at hvitis.dev on

Use Python to translate text from images to any language

Who is it for?

Have you ever found yourself looking for one of those bilingual books but none of them caught your interest? Maybe you thought that they re too expensive, after all - most of them are already on a public domain.

Or maybe you were abroad, went to a book store and found a really interesting book available only in the local language?

Scientific papers? That fits too!

This post explains how to translate photos, pictures that have text within them so that consists photos, screenshots, images, etc.

In order to initialize the project you will need a Google API key from Google Console which can take a bit to generate especially if you have never done it before. If you want to translate small batches or just a couple of images you may try to use other tools.

I love languages

In the following example you will see that we are going translate Siddhartha by Herman Hesse which is on public domain. The book we translate is in German and you can pick up just about any language google offers (one of 64). That is something that could really ease a language learning!

Copyrights

How does copyright work arrive in public domain and can be used freely?

Most of works people create is protected with Copyrights. In most of the countries they last a lifetime of an author plus 25-100 years. Depending on the country you may find books of foreign authors sooner - verify detailed world map to see exact timings.

You may wonder how to ask permission to translate a book, on public domain it is not required anymore (although if you are still wondering then just writing to Authors with a polite request will do). So can you translate public domain books? Yes, you can!

Once the Copyrights expire, works are becoming publicly available, they become part of a public domain. This is where we are going to get the book to translate - Siddhartha by Herman Hesse. On Archive.org we can see usage category which states: Usage Public Domain Mark 1.0

In order to show the full process I printed a few pages:

siddhartha-scanning-book

Let’s start

We need to first scan those pages. We will use mobile app called Microsoft Lens:

microsoft-office-lens-app

Once you download it, try scanning the pages. It has nice option of making the scanned documents flat and readable.

microsoft-office-lens-scanning-book

Once you finish scanning all the pages, select a black and white sharpening filter and sve those files.

Example scanned document should look like this:

Herman-Hesse

Now put all those scanned photos into one folder on your computer and let´s go to the code.

The intro

The script I wrote utilizes python packages for OCR scanning, translating and saving into docx file. It´s sort of a free document translation software and although it´s just a few python lines it can perform a full book translation and later you can generate pdf out of this docx.

Even if you have just pdf it can be tweaked and used as ebook translator. You can just use free pdf to png converter to obtain photos and run the script. This translation will only run if you´re online. The Python translate logic does not work offline because we are querying Google Translate API.

Maybe you´ve heard about other services like reverso, bing translator or deepl translator. Using this script you can have unlimited, ad free experience that you can tweak to your needs and make your own book translation app.

(If you do not want to use Google Services you may check apertium that also let´s you translate documents).

The code

Code is available here under GNU licence. You may want to check the usage and instalation steps.

The result is two files generated with original and translated text:

translation_result

Let me know what do you think and feel free to reach out if you need help.


_Tell me about your insights and leave a comment - you are most welcome to see more posts of this type just go to home page. Cover:_Photo by Beatriz Pérez Moya on Unsplash

Top comments (0)