A teenager who described Prince Harry as a “race traitor” in an online post has been sentenced to four years and three months in a young offender institution.

Michal Szewczuk, a university student, created an image of Harry with a pistol to his head against a blood-covered background.

The picture, which also featured a swastika, was shared on a far-right social media platform in August last year, a few months after the prince married Meghan Markle, who is of mixed race. The post included the phrase “See ya later race traitor”.

Szewczuk, 19, was ordered to be detained after pleading guilty to two counts of encouraging terrorism and five counts of possession of terrorist material, including the White Resistance Manual and an al-Qaida training manual.

He also wrote a blog, described as “extremely violent and aggressively misogynistic”, which attempted to justify the rape of women and children in the pursuit of an Aryan race.

Szewczuk, from Bramley, Leeds, was sentenced at the Old Bailey alongside Oskar Dunn-Koczorowski, 18, from Chiswick, west London, who had admitted two counts of encouraging terrorism.

Oskar Dunn-Koczorowski was sentenced to an 18-month detention and training order. Photograph: West Yorkshire police/PA

The judge, Rebecca Poulet QC, said the pair promoted the violent ideology of rightwing groups influenced by neo-Nazism.

Referring to the image of Harry, the judge said: “The posts I have seen and read are abhorrent as well as criminal by reason of their clear intention to encourage terrorist acts.”

She told Szewczuk: “Individuals were urged to go out and commit appalling acts of violence on others for no reason that can ever be understood by any right-thinking individuals.”

Sentencing Dunn-Koczorowski to an 18-month detention and training order, Poulet said: “You still hold deeply entrenched views in support of this extreme rightwing ideology.”

The defendants were both said to be of Polish descent, but had only known each other through online chatrooms.

They were arrested in December after posting images or links last summer influenced by extreme rightwing groups on Gab, a social media platform that attracts mainly far-right users.

One of the organisations, the Atomwaffen Division, was described as “a youth-driven, national socialist group at the extreme end of the revolutionary rightwing spectrum” and had been linked to five murders in the US since 2017, the court heard.

Dunn-Koczorowski, whose posts included support for the far-right terrorist Anders Breivik and the threat of the ethnic cleansing of Albanians, demonstrated a “highly radicalised and violent mindset”, the court was told.

His lawyer, David Kitson, admitted Dunn-Koczorowski’s mindset had not changed since the offences were committed, quoting from a medical report that said the teenager had a “lack of remorse” for his views and a “deeply entrenched ideology”.

The prosecutor, Naomi Parsons, said the posts, made across three accounts by the two individuals, “convey a message of the threat of and/or use of serious violence against others, in order to advance a political, ideological and racial cause (neo-Nazism) and in this way encourage terrorism”.

She told the court the targets included Jewish people, non-white people and anyone “perceived to be complicit in the perpetuation of multiculturalism”.

Dunn-Koczorowski was 17 at the time of the offences and living at home.

Szewczuk, who was arrested at his halls of residence during his first year studying computer science at Portsmouth University, had a “difficult and disordered upbringing” and had experienced depression “for a considerable period”, his lawyer, Adam Morgan, said.

The court heard he moved to the UK from Poland at the age of 10, living first in Northern Ireland and then England.

The defendants appeared in court via video link from Belmarsh prison and gave no reaction as they were sentenced.