Huge bit of code …..no idea how it works or where it finds a picture from your phone ?
As a general Point :
you should cut it down and get small chunks of it working , add print statements to check variables are doing what you think , then move on - it helps find out what is wrong.
Did you write this , copied usually often has issues or was intended for a different processor etc .