Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Image de-watermarking by Python programming OpenCV and Numpy Image processing Library

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces Python programming OpenCV and Numpy image processing library how to achieve picture de-watermarking, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

OpenCV + Numpy

The libraries to be used in this method: cv2, numpy. Cv2 is an image processing library based on OpenCV, which can corrode and inflate images; Numpy is a powerful library for dealing with matrix and dimension operations.

Brief introduction of function

Introduce the three basic functions of cv2: use cv2.imread (), cv2.imshow (), and cv2.imwrite () to read, display and save images, respectively.

Img = cv2.imread ('test.png') cv2.imshow (' test.png',img) cv2.waitKey (0) cv2.imwrite ('test_2.png', img)

For Numpy, use np.clip (), which is an intercept function that intercepts the part of the array that is less than or greater than a certain value and makes the intercepted part equal to a fixed value.

Np.clip (a, a_min, a_max, out=None):

Specific usage:

As you can see, all the numbers in the array x are limited to the range 0 and 5. Why introduce these functions and move on.

Color conversion

Going back to the beginning of this article, we want to remove the watermark from the document image.

In the image above, I selected three points, which correspond to the watermarks of white background, black font and gray.

What we need to do now is to find a way to convert the watermark to a white background. In other words, the pixels in [217217217] in the picture are converted into [255255255].

Of course, this [217217217] is not fixed, it's just a range. In order to facilitate adjustment, I selected some pixels and made a linear regression.

Want to make a change to the overall pixel color of the picture, the original black font as far as possible consistent with the original, and the watermark part must be ≥ 255, and then you can use np.clip () to limit the interval, so that all become [255255255].

Just do it.

Import cv2import numpy as npimg = cv2.imread ('test.png') new = np.clip (1.4057577998008846*img-38.33089999653017, 0255) .astype (np.uint8) cv2.imwrite (' removed.png', new)

Let's take a look at the adjusted effect (pre-conversion on the left and post-conversion on the right).

The processing effect is good, indicating that for this kind of document image watermark, the watermark can be easily removed through a few lines of Python code.

However, changing the overall picture color through linear regression will also affect the original black text, resulting in a slight change in color.

Then can we be simple and rough! Just change the color of the watermark?

You can try it.

PIL + itertools

PIL is also a Python image processing library, in which Image module is a common module in Python PIL image processing, and the basic functions of image operation are basically included in this module.

Itertools was previously called a zero-gap Python built-in library. Itertools.product is used to generate (product) of multiple lists and iterators.

Following the previous principle, we want to convert the pixels in the picture [217217217] to [255255255].

Let's be simple and rough, that is, if the pixel values add up to more than 600 (217-217-217), all the pixels will be changed to [255255255].

From itertools import productfrom PIL import Imageimg = Image.open ('test.png') width, height = img.sizefor pos in product (range (width), range (height)): if sum (img.getpixel (pos) [: 3]) > 600: img.putpixel (pos, (255255255)) img.save (' removed_1.png')

Run the results and compare them.

Compared with the first method, the naked eye does not see much difference.

Thank you for reading this article carefully. I hope the article "how to realize picture de-watermarking in Python programming OpenCV and Numpy image processing library" shared by the editor is helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 292

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report