Four nudist activists claimed in a lawsuit filed in November that the ban violated their constitutional right to free speech because nudity is a form of expression.
U.S. District Judge Edward Chen rejected that argument in an 18-page decision, saying that "nudity is not inherently expressive."
"Unlike the burning of a flag, burning of a draft card, or wearing a black armband in protest against the war, public nudity in and of itself is not commonly associated with expression of a particular message," Chen wrote.
The legislation was enacted by a 6-5 vote of the Board of Supervisors in December after hearings at which opponents of the ban stripped down inside and outside City Hall.
It was authored by Supervisor Scott Wiener, who said he was responding to complaints from residents and business owners in the Castro neighborhood, which is part of his district.
Chen also wrote that the ordinance does not conflict with state law and is not overly broad.
But he left the door open for the activists to amend their lawsuit after the law takes effect by arguing that the law is enforced against them in a discriminatory way.
To succeed in an amended lawsuit, the judge wrote, the nudists would have to show that police enforced the law against them selectively on the basis of their viewpoint.
Christina DiEdoardo, a lawyer for the plaintiffs, said, "Obviously, we're disappointed, but we are grateful to the judge for the time and thought he put into the case."
But DiEdoardo said the case is by no means over. She said she expects her clients to go ahead with an amended lawsuit in the future.
Chen gave a 45-day deadline for such a filing, and in the meantime, granted a request by City Attorney Dennis Herrera for dismissal of the lawsuit in its original form.
DiEdoardo said the activists may also consider filing an emergency appeal with the 9th U.S. Circuit Court of Appeals before Friday for a preliminary injunction, but she said no decision has been made on whether to take that action.
The law bans public nudity on streets and sidewalks, and in plazas, transit stations and vehicles, but makes an exception for children under the age of 5 and parades and fairs that have received permits, such as the annual Bay to Breakers race and the Folsom Street Fair.
Separate city laws restrict nudity in restaurants, public seating areas and in parks.
Herrera said, "Even though we're not surprised by Judge Chen's ruling, we're gratified by an outcome that affirms established case law and preserves reasonable exceptions for permitted events.
"We believed their challenge to be baseless, and we're grateful that the court agreed," Herrera said.