The 24-year-old was released by the Gills at the end of last season following their promotion to League One but has now been snapped up by the Cobblers to bolster their attacking options.
Manager Stuart Gray said: "Gary has been training with us for the last week or so and I think he has done well.
"I felt we needed another senior striker in the squad and he gives us something different to Adebayo Akinfenwa and Steve Guinan.
"He has good movement and can give us legs up front, but he also has a presence about him. He is another player who has won promotion from this league and I am delighted we have been able to agree terms with him. He has a first class attitude and he has good
experience of this level and the level above.
"He is a team player, he works his socks off for the benefit of the team and he can stretch defences with his work rate and his ability to run the channels and create space and opportunities for others."