I think I know what the problem is with this code.
You can lock and hide a *regular* page item, but not an image. An Image is something *inside* a regular page item; it's something you select with the *white* arrow, not the usual black selection arrow. If you want to hide and/or lock the image, you have to do so with its *parent* -- which is the object 'in' which the image appears.
You already retrieve the parent of all of your items near the top of the loop, but you only have to do so for the "Image" case.
(Post-Thought Edit) Hmm ... are you sure that 'allPageItems' include "Image" objects as well? After all, as I said above, an 'image' is *not* a regular 'page item'.