The only known photographs of mathematician and computing pioneer Ada Lovelace have been acquired by the National Portrait Gallery just before they were expected to be sold to a private buyer.