Oil spill incidents threaten the marine ecological environment. Detecting sea surface oil slicks by remote sensing images provides support for the efficient treatment of oil spills. This is important for sustainable marine development. However, traditional methods based on field analysis are time-consuming. Spectral indices lack applicability. In addition, traditional machine learning methods strictly rely on training and testing samples which are in short supply in oil spill images. Inspired by the spectral DNA encoding method, a spectral gene extraction (SGE) method was proposed to detect oil spills in hyperspectral images (HSI) and multispectral images (MSI). The SGE method contained a parameter and two strategies. The parameter of elimination was designed based on the population genetic frequency. It was used to control the number of spectral genes. The spectral gene extraction strategies, named largest in-class similarity (LIS) strategy and largest inter-class difference (LID) strategy, were proposed to mine the spectral genes by oil spill samples. The oil spills would be determined by calculating the similarity of the extracted spectral genes to the DNA encoded images. In this research, the SGE method was validated by two AVIRIS images of the Gulf of Mexico oil spill, one MODIS image of the Gulf of Mexico oil spill, and one Landsat 8 image of a Persian Gulf oil spill. The oil spills in different remote sensing images could be detected accurately by the proposed method in a small set of samples. Experimental results indicated that the proposed method was suitable for detecting marine oil spills in AVIRIS, MODIS, and Landsat 8 images. In addition, the SGE method with the LIS strategy was more suitable for detecting oil spills in HSI. Its proper elimination rates were 0.8~1.0. The SGE method with the LID strategy was more suitable for detecting oil spills in MSI. Its proper elimination rates were 0.5~0.7.