how to capture an image when an external trigger arised

Can you repost your code with code tags? Also, I notice you are not using global in your callback method. Python methods do not have scope of variables like C unless you say a variable is global before hand.

def callback (line):
    cap = 1

Is not valid python code. You have to do:

def callback (line):
    global cap
    cap = 1