Revenge pornography – sharing sexually explicit images of former partners without their consent – is to become a criminal offence punishable by up to two years in prison.

The legislation is to be introduced into the criminal justice and courts bill that is currently going through parliament, the justice secretary, Chris Grayling, has announced.

There has been mounting political pressure to outlaw the practice of humiliating former lovers by posting intimate pictures of them online.

Among those pressing for a change to the law has been the former culture secretary, Maria Miller. Others have argued that the problem is already covered by existing laws against obscenity or blackmail.

The new offence will cover the release of explicit images both online and in the form of printed pictures.

The justice secretary said: “The fact that there are individuals who are cruelly distributing intimate pictures of their former partners without their consent is almost beyond belief.

“We want those who fall victim to this type of disgusting behaviour to know that we are on their side and will do everything we can to bring offenders to justice.

“That is why we will change the law and make it absolutely clear to those who act in this way that they could face prison.”

The minister for women and equalities, Nicky Morgan, added: “Circulating intimate photos of an individual without their consent is never acceptable. People are entitled to expect a reasonable level of respect and privacy.

“It is right that those who do circulate these images are held to account, and that we educate young people to the hurt that can be caused by breaking this trust.”

The new legislation will mean that images posted to social networking sites such as Facebook and Twitter will be caught by the offence, as well as those that are shared via text message.

Images shared via email, on a website or the distribution of physical copies will also be caught, the MoJ said. Those convicted will face a maximum sentence of two years in prison.

The offence will apply to photographs or films that show people engaged in sexual activity or depicted in a sexual way or with their genitals exposed, where “what is shown would not usually be seen in public”.

Other laws may still be used to prosecute the sending of sexual images such as the Communications Act 2003 or the Malicious Communications Act 1988.