We address periodic-image errors arising from the use of periodic boundary conditions to describe systems that do not exhibit full three-dimensional periodicity. The difference between the periodic potential, as straightforwardly obtained from a Fourier transform, and the potential satisfying any other boundary conditions can be characterized analytically. In light of this observation, we present an efficient real-space method to correct periodic-image errors, based on a multigrid solver for the potential difference, and demonstrate that exponential convergence of the energy with respect to cell size can be achieved in practical calculations. Additionally, we derive rapidly convergent expansions for determining the Madelung constants of point-charge assemblies in one, two, and three dimensions.