Social networks and various websites provide a lot of tools for publishing and sharing digital images. However, publishing digital content online makes it vulnerable to illegal copying and distribution. An effective method for protecting images is watermarking technology, which embeds a logo or some kind of identifier into an image in an invisible way. If necessary, a watermark can be extracted from an image and used to prove authorship and ownership. A major challenge in designing image watermarking schemes is balancing embedding imperceptibility and resistance to various attacks. In this study, we propose a new robust and blind image watermarking algorithm that set a specific ratio of sum of discrete cosine transform (DCT) coefficient absolute values for each image block. We provide a balance between imperceptibility and robustness using a modern metaheuristic optimization algorithm called gradient-based optimizer, which finds the best vector of changes for mid-frequency DCT coefficients. An objective function of optimization algorithm is designed in such a way as to control the ratio of the sums of DCT coefficients absolute values to increase robustness and reduce a distortion level of an image block during embedding. Experimental results and comparison with some other image watermarking schemes demonstrate the high efficiency of the proposed algorithm in terms of imperceptibility and resistance to typical image processing attacks, such as changing brightness and contrast, sharpening, cropping, and others.