Researchers have scrutinized data hiding schemes in recent years. Data hiding in standard images works well, but does not provide satisfactory results in distortion-sensitive medical, military, or forensic images. This is because placing data in an image can cause permanent distortion after data mining. Therefore, a reversible data hiding (RDH) technique is required. One of the well-known designs of RDH is the difference expansion (DE) method. In the DE-based RDH method, finding spaces that create less distortion in the marked image is a significant challenge, and has a high insertion capacity. Therefore, the smaller the difference between the selected pixels and the more correlation between two consecutive pixels, the less distortion can be achieved in the image after embedding the secret data. This paper proposes a multilayer RDH method using the multilevel thresholding technique to reduce the difference value in pixels and increase the visual quality and the embedding capacity. Optimization algorithms are one of the most popular methods for solving NP-hard problems. The slime mould algorithm (SMA) gives good results in finding the best solutions to optimization problems. In the proposed method, the SMA is applied to the host image for optimal multilevel thresholding of the image pixels. Moreover, the image pixels in different and more similar areas of the image are located next to one another in a group and classified using the specified thresholds. As a result, the embedding capacity in each class can increase by reducing the value of the difference between two consecutive pixels, and the distortion of the marked image can decrease after inserting the personal data using the DE method. Experimental results show that the proposed method is better than comparable methods regarding the degree of distortion, quality of the marked image, and insertion capacity.