We present a new algorithm to analytically continue the self-energy of quantum many-body systems from Matsubara frequencies to the real axis. The method allows straightforward, unambiguous computation of electronic spectra for lattice models of strongly correlated systems from self-energy data that has been collected with state-of-the-art continuous time solvers within dynamical mean-field simulations. Using well-known analytical properties of the self-energy, the analytic continuation is cast into a constrained minimization problem that can be formulated as a quadratic programmable optimization with linear constraints. The algorithm is validated against exactly solvable finite-size problems, showing that all features of the spectral function near the Fermi level are very well reproduced and coarse features are reproduced for all energies. The method is applied to two well-known lattice problems, the two-dimensional Hubbard model at half filling, where the momentum dependence of the gap formation is studied, as well as a multiband model of NiO, for which the spectral function can be directly compared to experiment. Agreement with published results is very good.