原理
图像像素颜 {MOD}数据高位保留,低位存储图像数据。
我知道算法很简单啦没有鲁棒性啦…… 不听口亨。
添加
from PIL import Image
def addWaterMarking (pic , mark):
img = Image.open(pic).convert("RGB")
width, height = img.size
img_mark = Image.open(mark).convert("RGB")
img_mark = img_mark.resize((width, height))
img = img.point(lambda i: (int(i>>2))<<2)
img_mark = img_mark.point(lambda i: round(i/85))
img_pixels = list(img.getdata())
mark_pixels = list(img_mark.getdata())
newpixels = [0 for i in range(len(img_pixels))]
for index in range(len(img_pixels)):
list_temp = [];
for i in range(3):
list_temp.append(img_pixels[index][i] + mark_pixels[index][i])
newpixels[index] = tuple(list_temp)
imn = Image.new("RGB", (width, height))
imn.putdata(data = newpixels)
imn.save('static/output.png')
return
if __name__ == '__main__':
addWaterMarking('back2.jpg','mark.jpg')
检测
检测的时候会需要检测图像和水印图,返回检测图像是否带有水印。
from PIL import Image,ImageChops
def equal (im1, im2):
return ImageChops.difference(im1, im2).getbbox() is None
def testWaterMarking (pic , mark):
img = Image.open(pic).convert("RGB")
width, height = img.size
img_mark = Image.open(mark).convert("RGB")
img_mark = img_mark.resize((width, height))
img_pixels = list(img.getdata())
img_get = img.point(lambda i: (int(i&3))*85)
img_mark = img_mark.point(lambda i: round(i/85)*85)
print equal(img_get,img_mark)
return equal(img_get,img_mark)
if __name__ == '__main__':
print testWaterMarking('output.png','mark.jpg')
注 jpeg是有损压缩,中间存储要存为png等无损格式1