Any person convicted of an offense against the police of towns or against a bylaw of a town may, upon petition and proof of inability to pay the fine, be discharged by the selectmen, and the town shall be liable for prison charges if the prisoner is unable to pay them.