1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import PIL import cv2 from matplotlib import pyplot as plt import numpy as np from PIL import Image
def lsb_encoder(copyright_image, original_image): original_image = original_image.astype(np.uint8) copyright_image = copyright_image.astype(np.uint8) watermark = original_image.copy() copyright = copyright_image.copy() copyright[copyright < 200] = 1 copyright[copyright >= 200] = 0 for i in range(0, watermark.shape[0]): for j in range(0, watermark.shape[1]): watermark[i, j, :] = (watermark[i, j, :] // 2) * 2 for i in range(0, copyright.shape[0]): for j in range(0, copyright.shape[1]): watermark[i, j, 0] = watermark[i, j, 0] + copyright[i, j, 0] return watermark
def remove_transparency(img_pil, bg_colour=(255, 255, 255)): if img_pil.mode in ('RGBA', 'LA') or \ (img_pil.mode == 'P' and 'transparency' in img_pil.info): alpha = img_pil.convert('RGBA').split()[-1]
bg = Image.new("RGBA", img_pil.size, bg_colour + (255,)) bg.paste(img_pil, mask=alpha) return bg else: return img_pil
def lsb_decoder(watermark_image): watermark_image = watermark_image.astype(np.uint8) watermark = watermark_image.copy() watermark_copyright = (watermark % 2) * 255 return watermark_copyright
if __name__ == '__main__': img1 = Image.open('D:\\1.png') img11 = np.array(img1) img2 = Image.open('D:\\2.png') img22 = np.array(img2) plt.imshow(img11) plt.show() plt.imshow(img2) plt.show() img3 = lsb_encoder(img22, img11) plt.imshow(img3) plt.show() secret = lsb_decoder(img3) withalpha = PIL.Image.fromarray(secret).convert('RGB') plt.imshow(withalpha) plt.show()
|