Doesn't work with some PDFs

I tested this with two PDFs containing handwritten text. One of them ended up with every page rotated 90° and an empty page inserted after every page. The other didn't work at all because pdfimages didn't extrace a single image and then bash gave an error at the head of the next for loop.

When using pdftoppm instead of pdfimages it works with all kinds of PDFs I tested.