-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicludo.py
72 lines (59 loc) · 2.45 KB
/
picludo.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
from PIL import Image
import numpy as np
class Picludo:
"""Picludo object for encrypt and decript images using noise
Depends on:
Pillow - open and save images
Numpy - create and manipulate noise maps
Methods:
random_key(self, int, int)
split_pic(self, image_path, name_a, name_b)
join_pics(self, imageA_path, imageB_path, output_name)
"""
def __init__(self):
pass
@classmethod
def random_key(self, width, height):
"""NumPy array of given size
helper function
return uint8 data type RGB noise to keep 8 bit's per channel
"""
self.width = width
self.height = height
np_array = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
return np_array
@classmethod
def split_pic(self, original_image, name_a="out_A.bmp", name_b="out_B.bmp"):
"""saving noise images
outputs two noise files, both are needed to recover original.
name_a and name_b determine the file format, eg. BMP
using other than lossless bitmap formats may result
in unexpected behaviour
"""
input_img = Image.open(original_image)
array_from_input_img = np.asarray(input_img, dtype=np.uint8)
random_noise = self.random_key(input_img.size[0], input_img.size[1])
subtracted_array = array_from_input_img - random_noise
Image.fromarray(random_noise).save(name_a) # img from noise map
Image.fromarray(subtracted_array).save(name_b) # img after subtraction
@classmethod
def join_pics(self, file_A, file_B, output_name="recovered.bmp"):
"""joins two files in one
must be same pixel-size
output_name determines the file format, eg. BMP
order of input images is NOT important
"""
try: # avoid different image sizes
input_a = Image.open(file_A)
input_b = Image.open(file_B)
array_from_input_a = np.asarray(input_a, dtype=np.uint8)
array_from_input_b = np.asarray(input_b, dtype=np.uint8)
add_array = array_from_input_a + array_from_input_b
Image.fromarray(add_array).save(output_name)
except ValueError:
pass
# testing functions, original.bmp included in working directory
# uncomment next lines to see how it works
#factory = Picludo()
#factory.split_pic("original.bmp")
#factory.join_pics("out_B.bmp", "out_A.bmp", "recovered_original.jpg")