The Monarch of the Glen, one of Scotland's most famous paintings, could be saved for the nation if £4m can be raised in four months.

The National Galleries of Scotland (NGS) has entered into an agreement with the painting's owners, the drinks giant Diageo, with the aim of securing the world-renowned painting by Sir Edwin Landseer for permanent public display in Scotland.

Under the arrangement Diageo, which was due to sell the painting at auction, has agreed to gift roughly half the estimated market value of the painting to allow NGS the opportunity to buy the work.

The National Galleries have four months to raise the money needed, and allow the 1851 painting, much used in various types of advertising, to pass from private to public hands for the first time in its history.

If after four months the money cannot be raised its fate will once again be uncertain.

However, it will now no longer be sold at Christie's in December and, it is understood, Diageo are convinced it should be in public hands.

The deal was brokered after Sir John Leighton, director general of the NGS, saw that the painting was headed for the open market, and he contacted the drinks company - heavily criticised for its actions by leading Scottish banker Sir Angus Grossart - to initiate negotiations.

Sir John said that if he can raise the £4m and secure the painting, it would be a "very good deal" as he has received advice that it probably would have sold for more than £10m at auction.

Sir John said he would not be approaching the Scottish Government for money, and is still mulling a public appeal for funds.

He added: "I am quietly optimistic. A sale at auction is probably the worst case scenario, this is a great part sale agreement, especially if you think £8m is really a very conservative estimate of what it would reach at auction.

"Raising £4m is a challenge but we are optimistic."

He said the painting, if secured, would likely not be hung in the new galleries being planned for the garden-level entrance of the Scottish National Gallery - Landseer was English and Sir John said he would be chary of "inserting" the painting into the display of Scottish art.

It would likely be hung in the traditional National Gallery on the first floor.

Sir John added: "We are delighted with this grand gesture by Diageo which offers a once-in-a-lifetime opportunity for this major work to be acquired for the nation.

"The Monarch of the Glen is an iconic image which is famous across the world.

"The ideal home for such an important and resonant picture is the Scottish National Gallery where it can be enjoyed and admired by millions of visitors in the context of the nation’s unrivalled collection of Scottish, British and European art.

"We look forward to working with Diageo and our partners to ensure we achieve our ambition.”

David Cutter, Diageo’s senior director in Scotland said: "We are delighted to partner with the National Galleries of Scotland, to create the opportunity for The Monarch of the Glen to remain on public display in Scotland on a permanent basis.

"We look forward to working with the National Galleries of Scotland."

Fiona Hyslop, culture secretary. said: "I am pleased to welcome this plan to keep the iconic Monarch of the Glen on public display in Scotland for all to enjoy.

"Recent reaction to news of its auction underlined the importance of this painting and I’m pleased the National Galleries and Diageo have agreed a plan to ensure its long association with Scotland can continue."

The painting, on the walls of the National Museum of Scotland for many years, has been in private and corporate collections since it was painted in 1851.

Jussi Pylkkanen, global president of Christie’s, said: "This superb painting was purchased from Christie’s in 1916, and it is fitting exactly 100 years later in our 250th year it has the opportunity to find its permanent home in the National Galleries of Scotland."

Secretary of State for Scotland David Mundell said: "The Monarch of the Glen is a unique piece of Scotland’s rich cultural heritage and this is very welcome news in the National Galleries’ ongoing effort to keep this iconic painting on public display in Scotland for the future.

"The UK is home to some of the world’s great artworks, and it is important that they remain in our fantastic museums and galleries."