Dugan Chen's Homepage

Various Things

Converting Manga To Kindle Format

The following BASH converts directories full of bitmaps into Kindle books. You will find this useful if you enjoy scanned comic books, which are usually distributed as archives full of graphics files.

The script, as presented here, is for the Kindle DX, which has a screen resolution of 1280×824 and can display 16 colors. If you have a regular-sized Kindle, just change the resolution. If you have a more advanced Kindle that can display more colors, change the bit-depth. (The number of colors in the image is two to the power of the bit depth).

Run it in the directory where the image files are, and you’ll have a CBZ file containing appropriately sized images. The Kindle can read CBZ files. Just put them in its /documents folder.

#!/bin/bash

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

if [ ! -d temp ]
then
	mkdir temp
fi

for file in *
do
	information=`identify $file 2>&1`
	if [ $? -eq 0 ]
	then
		cp $file temp

		resolution=`echo $information | cut -d " " -f 3`
		width=`echo $resolution | cut -d "x" -f 1`
		height=`echo $resolution | cut -d "x" -f 2`

		if [ $width -gt $height ]
		then
			mogrify -strip -depth 4 -colorspace gray -type palette -resize 1280x824 temp/$file
		else
			mogrify -strip -depth 4 -colorspace gray -type palette -resize 1280x824 temp/$file
		fi
	fi
done

if [ -d temp ]
then
	if [ "`ls -A temp`" ]
	then
		zipfile=$(basename `pwd`).cbz > /dev/null
		cd temp
		zip $zipfile * > /dev/null
		cd ..
		cp temp/$zipfile .
	fi

	rm -r temp
fi

IFS=$SAVEIFS

To run it, you need zip and imagemagick installed. Imagemagick’s default scaling algorithms produce very good results.

And yes, I’m aware of Mangle. It works well.