使用Python(不带SciPy)检测照片中的特定水印

2024-05-12 14:29:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大量的图片(数十万张),每一张图片,我都需要说明它的右上角是否有水印。水印始终是相同的,并且位于相同的位置。它采用带符号和一些文本的功能区的形式。我正在寻找一种简单快速的方法来实现这一点,理想情况下,不使用SciPy(因为它在我使用的服务器上不可用——但它可以使用NumPy)

到目前为止,我已经尝试使用PIL和crop函数来隔离水印应该位于的图像区域,然后将直方图与RMS函数进行比较(参见http://snipplr.com/view/757/compare-two-pil-images-in-python/)。这样做不太好,因为两个方向都有很多错误。

任何想法都将非常感谢。谢谢


Tags: 方法函数crop文本numpy服务器pil情况
3条回答

水印的位置准确吗?如何将水印应用于背景图像?

我假设水印是部分加或乘函数。带水印的图像可能是这样计算的:

resultPixel = imagePixel + (watermarkPixel*mixinValue)

mixinValue将是0.0-1.0,因此您可以通过使用乘数(1-mixinValue)重新应用水印来完成混合。这将产生与水印匹配的像素。只需根据原始水印测试结果图像的颜色。

testPixel = resultPixel + (watermarkPixel*(1-mixinValue))
assert testPixel == watermarkPixel

当然,对带水印的图像进行压缩可能会导致testPixel出现一些变化。

另一种可能性是使用机器学习。我的背景是自然语言处理(不是计算机视觉),但是我试着用你的问题描述来创建一个训练和测试集,它看起来很有用(对看不见的数据100%准确)。

训练集

训练集由相同的带水印图像(正例)和不带水印图像(负例)组成。

测试集

测试集由不在训练集中的图像组成。

示例数据

如果你感兴趣,可以用example training and testing images试试。

代码:

完整版本可用as a gist。节选如下:

import glob

from classify import MultinomialNB
from PIL import Image


TRAINING_POSITIVE = 'training-positive/*.jpg'
TRAINING_NEGATIVE = 'training-negative/*.jpg'
TEST_POSITIVE = 'test-positive/*.jpg'
TEST_NEGATIVE = 'test-negative/*.jpg'

# How many pixels to grab from the top-right of image.
CROP_WIDTH, CROP_HEIGHT = 100, 100
RESIZED = (16, 16)


def get_image_data(infile):
    image = Image.open(infile)
    width, height = image.size
    # left upper right lower
    box = width - CROP_WIDTH, 0, width, CROP_HEIGHT
    region = image.crop(box)
    resized = region.resize(RESIZED)
    data = resized.getdata()
    # Convert RGB to simple averaged value.
    data = [sum(pixel) / 3 for pixel in data]
    # Combine location and value.
    values = []
    for location, value in enumerate(data):
        values.extend([location] * value)
    return values


def main():
    watermark = MultinomialNB()
    # Training
    count = 0
    for infile in glob.glob(TRAINING_POSITIVE):
        data = get_image_data(infile)
        watermark.train((data, 'positive'))
        count += 1
        print 'Training', count
    for infile in glob.glob(TRAINING_NEGATIVE):
        data = get_image_data(infile)
        watermark.train((data, 'negative'))
        count += 1
        print 'Training', count
    # Testing
    correct, total = 0, 0
    for infile in glob.glob(TEST_POSITIVE):
        data = get_image_data(infile)
        prediction = watermark.classify(data)
        if prediction.label == 'positive':
            correct += 1
        total += 1
        print 'Testing ({0} / {1})'.format(correct, total)
    for infile in glob.glob(TEST_NEGATIVE):
        data = get_image_data(infile)
        prediction = watermark.classify(data)
        if prediction.label == 'negative':
            correct += 1
        total += 1
        print 'Testing ({0} / {1})'.format(correct, total)
    print 'Got', correct, 'out of', total, 'correct'


if __name__ == '__main__':
    main()

示例输出

Training 1
Training 2
Training 3
Training 4
Training 5
Training 6
Training 7
Training 8
Training 9
Training 10
Training 11
Training 12
Training 13
Training 14
Testing (1 / 1)
Testing (2 / 2)
Testing (3 / 3)
Testing (4 / 4)
Testing (5 / 5)
Testing (6 / 6)
Testing (7 / 7)
Testing (8 / 8)
Testing (9 / 9)
Testing (10 / 10)
Got 10 out of 10 correct
[Finished in 3.5s]

您始终可以使用restb.ai的Specialized Image RecognitionAPI来自动执行水印检测过程。

import requests

url = "https://api.restb.ai/segmentation"

querystring = {"client_key":"your-free-key-here","model_id":"re_logo","image_url":"http://demo.restb.ai/img/gallery/realestate/logos-watermarks/re_logo-1.jpg"}

response = requests.request("GET", url, params=querystring)

print(response.text)

Screenshot of Logo & Watermark detection demo

相关问题 更多 >