Sure that you still have a line like
nr_of_pages_per_pdf_book = 100;
If that is the case, it can't hurt to put a line like
just before the line
function outputImage(image, outdir, pageNo, rectNo)
Anyway it's strange. The file works fine for me.