So, you want to write a book (such as Effective CMake for instance) and of course you also want to add nice images to your book. For technical books Leanpub suggests that your images should have a resolution of 468px x 648px at 72 ppi. Unfortunately, 72 points per inch on printed paper won’t quite give you crisp and clear images. While this value may be sufficient for screen readers, usually a value of at least 300 ppi for printed documents is absolutely mandatory.

Therefore, in order to create images with 300 ppi you need a calculated image resolution of 1949px x 2705px. Sadly, those images will still look very tiny in the pdf file generated by Leanpub. So, you may just double the resolution of the images while keeping the ppi at the same value which results in a image resolution of 3898px x 5409px.

While starting to write on the aforementioned book Effective CMake, I noticed that Amazon Kindle unfortunately isn’t able to deal with transparent images as others already found out. Hence, I needed to add a white background to my generated png images. Since this workflow easily becomes quite tedious when doing by hand, I’ve hacked a simple bash script, which automates all of this using ImageMagick:

#!/bin/bash

for in_name in $(ls *.pdf); do
  out_name=${in_name%.*}.png # target name without extension
  convert -units PixelsPerCentimeter -density 300 -resize 3898x \
  -strip +repage -flatten ${in_name} ${out_name}
  pngcrush -ow ${out_name}
  mv ${out_name} ../manuscript/images
  done

In order to reduce the overall file size of the generated book files, I also used a nice little tool called pngcrush which is able to reduce any given png file in size while maintaining its image quality.