Img_to_array is not defined

That first img variable loses scope outside the with block, doesn’t it?
Not sure it’s related to your issue (I don’t have any real experience with PIL) but that looks like it might not be doing what you think it does, as this line -
img=img.resize(.....
is calling resize on an uninitialised variable, isn’t it?