Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need the python code for Visual Cryptography.
An image is spilted into two using the visual cryptographic technique. And these two image share overlapped together to get the original image.

If the below pasted code is correct, I need code for the overlapping section of the image shares.

What I have tried:

I tried the image spilting section



from PIL import Image

import random
import sys


image = Image.open(r"C:\Users\JAISON\Desktop\miniproject\Visual cryptography\unicode-text.png")
image = image.convert('1')

outfile1 = Image.new("1", [dimension * 2 for dimension in image.size])

outfile2 = Image.new("1", [dimension * 2 for dimension in image.size])

for x in range(0, image.size[0], 2):
for y in range(0, image.size[1], 2):
sourcepixel = image.getpixel((x, y))
assert sourcepixel in (0, 255)
coinflip = random.random()
if sourcepixel == 0:
if coinflip < .5:
outfile1.putpixel((x * 2, y * 2), 255)
outfile1.putpixel((x * 2 + 1, y * 2), 0)
outfile1.putpixel((x * 2, y * 2 + 1), 0)
outfile1.putpixel((x * 2 + 1, y * 2 + 1), 255)

outfile2.putpixel((x * 2, y * 2), 0)
outfile2.putpixel((x * 2 + 1, y * 2), 255)
outfile2.putpixel((x * 2, y * 2 + 1), 255)
outfile2.putpixel((x * 2 + 1, y * 2 + 1), 0)
else:
outfile1.putpixel((x * 2, y * 2), 0)
outfile1.putpixel((x * 2 + 1, y * 2), 255)
outfile1.putpixel((x * 2, y * 2 + 1), 255)
outfile1.putpixel((x * 2 + 1, y * 2 + 1), 0)

outfile2.putpixel((x * 2, y * 2), 255)
outfile2.putpixel((x * 2 + 1, y * 2), 0)
outfile2.putpixel((x * 2, y * 2 + 1), 0)
outfile2.putpixel((x * 2 + 1, y * 2 + 1), 255)
elif sourcepixel == 255:
if coinflip < .5:
outfile1.putpixel((x * 2, y * 2), 255)
outfile1.putpixel((x * 2 + 1, y * 2), 0)
outfile1.putpixel((x * 2, y * 2 + 1), 0)
outfile1.putpixel((x * 2 + 1, y * 2 + 1), 255)

outfile2.putpixel((x * 2, y * 2), 255)
outfile2.putpixel((x * 2 + 1, y * 2), 0)
outfile2.putpixel((x * 2, y * 2 + 1), 0)
outfile2.putpixel((x * 2 + 1, y * 2 + 1), 255)
else:
outfile1.putpixel((x * 2, y * 2), 0)
outfile1.putpixel((x * 2 + 1, y * 2), 255)
outfile1.putpixel((x * 2, y * 2 + 1), 255)
outfile1.putpixel((x * 2 + 1, y * 2 + 1), 0)

outfile2.putpixel((x * 2, y * 2), 0)
outfile2.putpixel((x * 2 + 1, y * 2), 255)
outfile2.putpixel((x * 2, y * 2 + 1), 255)
outfile2.putpixel((x * 2 + 1, y * 2 + 1), 0)

outfile1.save('out1.jpg')
outfile2.save('out2.jpg')
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900