A detective who was fired for taking and keeping naked pictures of a rape victim — the step-daughter of rocker David Bowie — and of a woman who was busted for a traffic infraction is entitled to his pension, a state appeals court ruled yesterday.

Richard Vecchio was a 19-year veteran of the NYPD and a father of five when he was booted off the force after his colleagues found the creepy keepsakes.

One was a set of 15 nude photos of model Stacia Lipka — the daughter of Bowie’s ex-wife Angela — in the hospital after she’d been raped. The other was a picture of a bare breast of a female suspect with a piercing that he claimed he might need for evidence later on.

Vecchio was found not guilty of criminal charges in the Lipka case, but was convicted at a departmental trial and then fired.

Vecchio filed suit challenging the finding of wrongdoing and contending his penalty was too harsh. In a 4-1 ruling, the state Appellate Division upheld the picture charges, but tossed some of the lesser findings.

They ordered his case to go back before the police commissioner for resentencing — but said that even if Ray Kelly still decides he should be terminated, Vecchio “should be permitted to apply for a vested interest retirement.”

They said that despite his “unseemly” behavior, Vecchio had an “otherwise exemplary career” that garnered him 12 medals for excellent police duty, and that his “innocent family” would suffer without the retirement benefits.

The dissenting judge, John Sweeny, said his colleagues were going too far. “The majority cannot question that the commissioner engaged in anything other than a careful review of this case, including the effect the penalty would have on” Vecchio, Sweeny wrote.

Vecchio had already cost the city plenty. The city paid Lipka an $80,000 settlement, and the woman who was arrested for the traffic infraction was paid $70,000.