The production cross section of a top quark pair in association with a photon is measured in proton-proton collisions at a center-of-mass energy of 13 TeV. The data set, corresponding to an integrated luminosity of 137 fb$^{-1}$, was recorded by the CMS experiment during the 2016-2018 data taking of the LHC. The measurements are performed in a fiducial volume defined at the particle level. Events with an isolated, highly energetic lepton, at least three jets from the hadronization of quarks, among which at least one is b tagged, and one isolated photon are selected. The inclusive fiducial $\mathrm{t\overline{t}}\gamma$ cross section, for a photon with transverse momentum greater than 20 GeV and pseudorapidity $\lvert \eta\rvert$$\lt$ 1.4442, is measured to be 798 $\pm$ 7 (stat) $\pm$ 48 (syst) fb, in good agreement with the prediction from the standard model at next-to-leading order in quantum chromodynamics. The differential cross sections are also measured as a function of several kinematic observables and interpreted in the framework of the standard model effective field theory (EFT), leading to the most stringent direct limits to date on anomalous electromagnetic dipole moment interactions of the top quark and the photon.