We propose a method for preparing mixed quantum states of arbitrary dimension D (D≥2) which are codified in the discretized transverse momentum and position of single photons, once they are sent through an aperture with D slits. Following our previous technique we use a programmable single phase-only spatial light modulator (SLM) to define the aperture and set the complex transmission amplitude of each slit, allowing the independent control of the complex coefficients that define the quantum state. Since these SLMs give us the possibility to dynamically vary the complex coefficients of the state during the measurement time, we can generate not only pure states but also quantum states compatible with a mixture of pure quantum states. Therefore, by using these apertures varying on time according to a probability distribution, we have experimentally obtained D-dimensional quantum states with purities that depend on the parameters of the distribution through a clear analytical expression. This fact allows us to easily customize the states to be generated. Moreover, the method offers the possibility of working without changing the optical setup between pure and mixed states, or when the dimensionality of the states is increased. The obtained results show a quite good performance of our method at least up to dimension D=11, being the fidelity of the prepared states F>0.98 in every case.