KRAKOW, Poland — A judge in Poland on Friday turned down a request by the United States for the extradition of the filmmaker Roman Polanski, who is wanted over a 1977 conviction for having sex with a 13-year-old girl.

At a hearing in Krakow, Judge Dariusz Mazur ruled that turning over Mr. Polanski would be an “obviously unlawful” deprivation of liberty and that California would be unlikely to provide humane living conditions for the filmmaker, who is 82.

“I am very happy that the case is ending,” Mr. Polanski said at a news conference in Krakow after the ruling, the latest development in a 38-year trans-Atlantic legal controversy. “This has been a tremendous burden on me and my family.”

Mr. Polanski, a citizen of France and Poland, has been working on a film in Poland about Alfred Dreyfus, a French Army captain who was wrongly convicted of spying for Germany in 1894.