1. You don't know if the weight loss was intentional or is a positive thing for the person. If a person didn't tell you that they wanted to lose weight, you don't know whether their weight loss is a good thing. They could have has lost weight due to illness, grief, stress, an eating disorder, or just incidentally.
But what if the person was fat and looks better or healthier now?
It doesn't matter. Although we associate eating disorders with very skinny women, people of all sizes can have them. And while some people believe that being thin is more important than anything else - that losing weight is a good part of being sick or that people should forego treatment if a side effect is weight gain - everyone doesn't think that way. How you would like it if someone kept insisting, "But you should be happy you're in the hospital! You don't have to go to work!"
2. It can make someone wonder what you thought of them before they lost weight. It's easy to tell someone you like their new haircut or the way they rearranged their bedroom without indicating anything was wrong before. But in a culture where thinner = better, it's hard to compliment someone on losing weight without indicating that they "needed" to lose weight in the first place. The person might feel judged, like you didn't approve of them before they lost weight.
3. A person can't leave their body at home. A person doesn't choose to show off their body the same way they might show off their latest artwork or juggling trick. If someone doesn't want comments on their weight loss for any reason, they don't have the option of not showing you their body.
By automatically assuming that weight loss is good, we promote the idea that thin is good and fat is bad, which is untrue, prejudiced, and very hurtful. Your best bet is to save the weight-loss compliments for people who you actually say that they want to lose weight. There are plenty of other compliments to give.