A method to separate the non-resonant inelastic X-ray scattering signal of a micro-metric sample contained inside a diamond anvil cell (DAC) from the signal originating from the high-pressure sample environment is described. Especially for high-pressure experiments, the parasitic signal originating from the diamond anvils, the gasket and/or the pressure medium can easily obscure the sample signal or even render the experiment impossible. Another severe complication for high-pressure non-resonant inelastic X-ray measurements, such as X-ray Raman scattering spectroscopy, can be the proximity of the desired sample edge energy to an absorption edge energy of elements constituting the DAC. It is shown that recording the scattered signal in a spatially resolved manner allows these problems to be overcome by separating the sample signal from the spurious scattering of the DAC without constraints on the solid angle of detection. Furthermore, simple machine learning algorithms facilitate finding the corresponding detector pixels that record the sample signal. The outlined experimental technique and data analysis approach are demonstrated by presenting spectra of the Si L-2,L-3-edge and O K-edge of compressed alpha-quartz. The spectra are of unprecedented quality and both the O K-edge and the Si L-2,L-3-edge clearly show the existence of a pressure-induced phase transition between 10 and 24 GPa.