forked from ravi5175/IAWT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_io.py
109 lines (94 loc) · 2.87 KB
/
img_io.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 7 19:23:34 2020
Project-Image Authentication Using Water Marking Technique
Section-Image I/0
@author: RAVI
"""
import cv2
import os
from matplotlib import pyplot as plt
import sys
import math
import itertools
import random
import key_io as skey
from PIL import Image
UPLOAD_FOLDER='/image/upload'
ALLOWED_EXTENSIONS={'png','jpg','jpeg','gif'}
def readLSB(image, width, binary, order):
bit = []
px = image.load()
k = 0
cur = 0
for pos in order:
i = pos / width
j = pos % width
if (binary):
cur |= (px[i % image.width, j % image.height] & 1) << k
else:
cur |= (px[i % image.width, j % image.height][0] & 1) << k
k += 1
if (k >= 8):
bit.append(cur)
k = 0
cur = 0
if (k > 0):
bit.append(cur)
return bit
def extract_lsb(inputpath, outputpath,key='qwerty'):
cover = Image.open(inputpath)
lsb = Image.new("1", cover.size);
px_cover = cover.load()
px_lsb = lsb.load()
#key = input("Enter Key: ")
seed = 0
key_size = len(key)
for i in range(key_size):
seed += ord(key[i])
cipher = readLSB(cover, cover.width, 0, skey.shuffle_order(cover.width * cover.height, seed))
plain = skey.decrypt(cipher, key)
k = 0
cur = 0
positions = skey.shuffle_order(cover.width * cover.height, 0)
for pos in positions:
i = pos / cover.width
j = pos % cover.width
px_lsb[i, j] = ((plain[int(k / 8)] >> (k % 8)) & 1)
k = (k + 1)
lsb.save(outputpath);
lsb.show();
def insert_lsb(inputpath, watermarkpath, outputpath, key):
cover = Image.open(inputpath)
watermark = Image.open(watermarkpath).convert("1")
output = Image.new(cover.mode, cover.size)
px_cover = cover.load()
px_watermark = watermark.load()
px_output = output.load()
plain = readLSB(watermark, cover.width, 1, skey.shuffle_order(cover.width * cover.height, 0))
#key = input("Enter Key: ")
cipher = skey.encrypt(plain, key)
k = 0
cur = 0
mod = watermark.width * watermark.height
seed = 0
key_size = len(key)
for i in range(key_size):
seed += ord(key[i])
positions = skey.shuffle_order(cover.width * cover.height, seed)
for pos in positions:
i = pos / cover.width
j = pos % cover.width
p = list(px_cover[i, j])
p[0] = (p[0] & 0b11111110) | ((cipher[int(k / 8)]>>(k % 8)) & 1)
k = (k + 1) % mod
px_output[i, j] = tuple(p)
output.save(outputpath)
output.show()
return output
def psnr(watermarkedcover, plaincover):
return 20 * math.log10(255/skey.rms(watermarkedcover, plaincover))
def print_psnr(watermarkedpath, plainpath):
watermarked = Image.open(watermarkedpath)
plain = Image.open(plainpath)
return str(psnr(watermarked, plain))