Below is the code in python to implement a (2,4)2VCS scheme. Here "img.jpg" is the image that is to be shared. For testing purpose, save an image with the same name in the directory that you'll be running the program in. share1, share2, share3, share4 are the 4 shares that are generated. If you combine any of these two using the cv2.add() function in python, you'll get the secret image. I have added the code for the combination below as well. The results are saved as res12 and so on. The program takes around 1-5 minutes depending on the size of the image.
import numpy as np import cv2 import random e1 = cv2.getTickCount() # your code execution #loading image in grayscale : 0 image = cv2.imread('img.jpg',0) #converting the image to bw; threshold determined by Otsu's method (thresh, img_s) = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) #saving bw image cv2.imwrite('bw_image.jpg', img_s) #size of the image size = img_s.shape h = size[0] b = size[1] #print img_s.dtype #size of share : proportional to pixel exapnsion H = h*2 B = (b/2)*3 #creating blank shares share1 = np.zeros((H,B),np.uint8) cv2.imwrite('share1.jpg', share1) share2 = np.zeros((H,B),np.uint8) cv2.imwrite('share2.jpg', share2) share3 = np.zeros((H,B),np.uint8) cv2.imwrite('share3.jpg', share3) share4 = np.zeros((H,B),np.uint8) cv2.imwrite('share4.jpg', share4) #white : 255; black : o l = 0 w = 255 #basis matrices : s0 = [[l,l,l,w,w,w], [l,l,l,w,w,w], [l,l,l,w,w,w], [l,l,l,w,w,w]] s1 = [[l,l,l,w,w,w], [l,w,w,l,l,w], [w,l,w,l,w,l], [w,w,l,w,l,l]] s2 = [[l,l,l,w,w,w], [l,l,w,l,w,w], [l,l,w,w,l,w], [l,l,w,w,w,l]] #for generation of random permutation arr = random.sample([0,1,2,3,4,5],6) #i moves by 1, j moves by 2 since 2 consecutive pixels are being encrypted each time for i in range(0,h): for j in range(0,(b-1)): arr = random.sample([0,1,2,3,4,5],6) p1 = img_s[i,j] p2 = img_s[i,j+1] ps1 = i*2 ps2 = (j/2)*3 print arr if (p1==255 and p2==255): #both pixels are black #share1 share1[ps1,ps2] = s1[0][arr[0]] share1[ps1,(ps2+1)] = s1[0][arr[1]] share1[ps1,(ps2+2)] = s1[0][arr[2]] share1[(ps1+1),ps2] = s1[0][arr[3]] share1[(ps1+1),(ps2+1)] = s1[0][arr[4]] share1[(ps1+1),(ps2+2)] = s1[0][arr[5]] #share2 share2[ps1,ps2] = s1[1][arr[0]] share2[ps1,(ps2+1)] = s1[1][arr[1]] share2[ps1,(ps2+2)] = s1[1][arr[2]] share2[(ps1+1),ps2] = s1[1][arr[3]] share2[(ps1+1),(ps2+1)] = s1[1][arr[4]] share2[(ps1+1),(ps2+2)] = s1[1][arr[5]] #share3 share3[ps1,ps2] = s1[2][arr[0]] share3[ps1,(ps2+1)] = s1[2][arr[1]] share3[ps1,(ps2+2)] = s1[2][arr[2]] share3[(ps1+1),ps2] = s1[2][arr[3]] share3[(ps1+1),(ps2+1)] = s1[2][arr[4]] share3[(ps1+1),(ps2+2)] = s1[2][arr[5]] #share4 share4[ps1,ps2] = s1[3][arr[0]] share4[ps1,(ps2+1)] = s1[3][arr[1]] share4[ps1,(ps2+2)] = s1[3][arr[2]] share4[(ps1+1),ps2] = s1[3][arr[3]] share4[(ps1+1),(ps2+1)] = s1[3][arr[4]] share4[(ps1+1),(ps2+2)] = s1[3][arr[5]] elif (p1==0 and p2==0): #both pixels are white #share1 share1[ps1,ps2] = s0[0][arr[0]] share1[ps1,(ps2+1)] = s0[0][arr[1]] share1[ps1,(ps2+2)] = s0[0][arr[2]] share1[(ps1+1),ps2] = s0[0][arr[3]] share1[(ps1+1),(ps2+1)] = s0[0][arr[4]] share1[(ps1+1),(ps2+2)] = s0[0][arr[5]] #share2 share2[ps1,ps2] = s0[1][arr[0]] share2[ps1,(ps2+1)] = s0[1][arr[1]] share2[ps1,(ps2+2)] = s0[1][arr[2]] share2[(ps1+1),ps2] = s0[1][arr[3]] share2[(ps1+1),(ps2+1)] = s0[1][arr[4]] share2[(ps1+1),(ps2+2)] = s0[1][arr[5]] #share3 share3[ps1,ps2] = s0[2][arr[0]] share3[ps1,(ps2+1)] = s0[2][arr[1]] share3[ps1,(ps2+2)] = s0[2][arr[2]] share3[(ps1+1),ps2] = s0[2][arr[3]] share3[(ps1+1),(ps2+1)] = s0[2][arr[4]] share3[(ps1+1),(ps2+2)] = s0[2][arr[5]] #share4 share4[ps1,ps2] = s0[3][arr[0]] share4[ps1,(ps2+1)] = s0[3][arr[1]] share4[ps1,(ps2+2)] = s0[3][arr[2]] share4[(ps1+1),ps2] = s0[3][arr[3]] share4[(ps1+1),(ps2+1)] = s0[3][arr[4]] share4[(ps1+1),(ps2+2)] = s0[3][arr[5]] else: #b/w #share1 share1[ps1,ps2] = s2[0][arr[0]] share1[ps1,(ps2+1)] = s2[0][arr[1]] share1[ps1,(ps2+2)] = s2[0][arr[2]] share1[(ps1+1),ps2] = s2[0][arr[3]] share1[(ps1+1),(ps2+1)] = s2[0][arr[4]] share1[(ps1+1),(ps2+2)] = s2[0][arr[5]] #share2 share2[ps1,ps2] = s2[1][arr[0]] share2[ps1,(ps2+1)] = s2[1][arr[1]] share2[ps1,(ps2+2)] = s2[1][arr[2]] share2[(ps1+1),ps2] = s2[1][arr[3]] share2[(ps1+1),(ps2+1)] = s2[1][arr[4]] share2[(ps1+1),(ps2+2)] = s2[1][arr[5]] #share3 share3[ps1,ps2] = s2[2][arr[0]] share3[ps1,(ps2+1)] = s2[2][arr[1]] share3[ps1,(ps2+2)] = s2[2][arr[2]] share3[(ps1+1),ps2] = s2[2][arr[3]] share3[(ps1+1),(ps2+1)] = s2[2][arr[4]] share3[(ps1+1),(ps2+2)] = s2[2][arr[5]] #share4 share4[ps1,ps2] = s2[3][arr[0]] share4[ps1,(ps2+1)] = s2[3][arr[1]] share4[ps1,(ps2+2)] = s2[3][arr[2]] share4[(ps1+1),ps2] = s2[3][arr[3]] share4[(ps1+1),(ps2+1)] = s2[3][arr[4]] share4[(ps1+1),(ps2+2)] = s2[3][arr[5]] #2pixels has been encoded j+=2 i+=1 pass #generating shares cv2.imwrite('share1.jpg', share1) cv2.imwrite('share2.jpg', share2) cv2.imwrite('share3.jpg', share3) cv2.imwrite('share4.jpg', share4) e2 = cv2.getTickCount() time = (e2 - e1)/ cv2.getTickFrequency() #combining the shares : res12 = cv2.add(share1,share2) cv2.imwrite('res12.jpg', res12) res23 = cv2.add(share2,share3) cv2.imwrite('res23.jpg', res23) res34 = cv2.add(share3,share4) cv2.imwrite('res34.jpg', res34) print time
_______________________________________________ Cryptography-dev mailing list Cryptography-dev@python.org https://mail.python.org/mailman/listinfo/cryptography-dev