Use Python to translate text from images to any language
Published: 2021-06-14 21:55:44

---
title: Use Python to translate text from images to any language
published: true
date: 2021-05-27 00:00:00 UTC
tags: 
canonical_url: https://hvitis.dev/how-to-translate-books-for-free-to-any-language-with-python
---

## Who is it for?

Have you ever found yourself looking for one of those [bilingual books](https://clasicosbilingues.com/site/) 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](https://en.wikipedia.org/wiki/List_of_countries%27_copyright_lengths) 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](http://www.publicdomainsherpa.com/public-domain-books.html). This is where we are going to get the book to translate - [Siddhartha by Herman Hesse](https://archive.org/details/bub_gb_x-7OAAAAMAAJ). 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](https://hvitis.dev/static/f53f7adedc614092758217e09fc7e785/8bc0c/siddhartha-scanning-book.jpg "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](https://hvitis.dev/static/174c547c5dde2f4304c3daf2b5610d74/941ef/microsoft-office-lens-app.png "microsoft-office-lens-app")](https://play.google.com/store/apps/details?id=com.microsoft.office.officelens&hl=en_US&gl=US)

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](https://hvitis.dev/static/903f164df446214df0b5514327702417/dcf16/microsoft-office-lens-scanning-book-2.jpg "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](https://hvitis.dev/static/36aad37aaf375722ebb0df0aea3eacac/8bc0c/Herman-Hesse-Siddhartha.jpg "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](https://github.com/hvitis/translate-anything-with-python) 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](https://pdf2png.com) to obtain photos and run [the script](https://github.com/hvitis/translate-anything-with-python). 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](https://www.apertium.org/index.eng.html#?dir=eng-epo&q=) that also let´s you translate documents).

# The code

Code is available [here](https://github.com/hvitis/translate-anything-with-python/blob/main/main.py) under GNU licence. You may want to check the [usage and instalation steps](https://github.com/hvitis/translate-anything-with-python#instruction).

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

![translation_result](https://hvitis.dev/static/fca6fb8fa741788535e0d0549970dfd4/4d236/translation_result.png "translation\_result")

Let me know what do you think and feel free to [reach out](https://hvitis.dev/contact) 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](https://hvitis.dev). Cover:_Photo by [Beatriz Pérez Moya](https://unsplash.com/@beatriz_perez?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

<!-- DOCS -->