Emotion has a lot to do with it. When you're excited (ie: object of your affection is infront of you), your sympathetic nervous system (which deals with stressful situations) takes over. As a result, one of the automatic reactions would be the dilation of the pupils. On the other hand, when you're bored, laidback or relaxed, the parasympathetic nervous system takes over, and pupils will contract.