Readable math formulas on Kindle

Recently I’ve gained back interest in mathematics, and as a result I downloaded found a few math books (in EPUB format) to put on my Kindle.

To be honest, the reading experience on the Kindle almost extinguished my rekindled (excuse the pun) interest. Look at how bad it renders a page of “Fractal Geometry: Mathematical Foundations and Applications” by Kenneth Falconer:

Sample Formula

While texts are okay, formulas are comparably small and soft. Diagrams, on the other hand, are pretty decent. Even formulas inside diagrams look fine.

Sample Formula

I noticed that although both formulas and diagrams are images, formulas are usually inline, which are much smaller than diagrams. I then tried to extract the formula images, enlarge them and thicken text strokes.

Behold:

Sample Formula

For a quick summary of the steps, skip to the tl;dr.

What you will need#

  • The ebook file you want to fix — Mine is in the EPUB format, but the steps should be similar for other ebook types.
  • An ebook reader software — I’m using Calibre, a free but powerful ebook reader/manager.
  • ImageMagick — a command line tool to edit images in batch.
  • [Optional] Photoshop — I used this to test and preview my image enhancements. You can use GIMP if you prefer free & open-source software.

Extract formula images#

This is easy if you’re dealing with EPUBs: renaming your ebook from .epub to .zip, then extracting it. This is the contents of the extracted zip:

fractal_geometry.zip
└── OPS/
    ├── images/
    │   ├── c01-math-0001.png
    │   └── ...
    ├── c01.xhtml
    └── ...

As you can see, the ebook is consisted of .xhtml text layouts and .png images in a folder aptly named images. Luckily, since all formula images have math in their names, we can copy them to another folder for processing later with a single command:

$ cp images/*math*.png copied-images/

Proof-of-concept using Photoshop#

To figure out how to enhance the images, I opened a random formula in Photoshop:

Sample Formula

After loading the image, I realized that all the filters were greyed out:

Photoshop filters greyed out

This usually means something funky is happening to the color mode. Straight away, I found the mode to be Indexed Color, and a simple switch to RGB would let my apply filters again.

Indexed color mode

Next, I enlarged the image, then used Smart Sharpen to thicken the text:

Smart Sharpen

Finally, I replaced the modified image, zipped and renamed the ebook folder back to .epub. This looked promising:

Sample Formula

Batch processing all formulas#

Theoretically I could record a macro and apply the steps above to the remaining formula images in Photoshop, but its Batch Process feature didn’t let me export to PNG natively. Moreover, it was super slow and probably wouldn’t be able to process the book’s 12k formula images in a reasonable amount of time.

This is where ImageMagick comes into the picture. Similar to Photoshop, it can make modifications to photos, but its ability to run from the CLI makes ImageMagick shine. Using simple commands, we can process a large batch of images with great speed.

After some tinkering, I came up with the following command to achieve results similar to what I did with Photoshop earlier:

$ magick mogrify -path copied-images -colorspace rgb -adaptive-resize 125% -unsharp 0x5 processed-images/ *.png

This means, for each PNG image in copied-images, we change the color space to RGB, make it 125% bigger, thicken strokes with unsharp mask, then save the edited image in processed-images.

After this, I copied the improved diagram images back into the ebook, and achieved the results shown earlier.

tl;dr#

  1. Rename your “.epub” ebook file to “.zip”, then extract it.

  2. Inside, copy math formulas to another folder:

$ cp images/*math*.png copied-images/
  1. Improve the formula images with ImageMagick:
$ magick mogrify -path copied-images -colorspace rgb -adaptive-resize 125% -unsharp 0x5 processed-images/ *.png
  1. Copy the processed images back to images/.

  2. Zip back the folder and rename it back to “.epub”.

  3. Use Calibre to import the book to the Kindle.

  4. Profit.

Each ebook’s file structure is different, so your mileage might vary. Please refer to the solution reproduction steps discussed above for more info.

Next steps#

Although I did enjoy reading my improved math ebooks on the Kindle for some time, I’ve bought an iPad recently and reading PDFs on that was a significantly better experience.

However, this can still be useful to other people who don’t own iPads or those who insist on reading on the Kindle, so there are ideas that I might try if there are enough interest:

  • A script to automate this process.
  • Using machine learning to detect math formulas instead of relying on file names.