-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.py
More file actions
31 lines (23 loc) · 879 Bytes
/
Message.py
File metadata and controls
31 lines (23 loc) · 879 Bytes
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
from PIL import Image
import numpy as np
import komm as komm
test_image_file_name = "test_image.jpeg"
saved_test_image = "save_image.jpeg"
class Image_message:
def __init__(self,file_name):
"""Load data from file and get size and pixel data (as 'image_bits')
"""
try:
image = Image.open(file_name)
except IOError:
print("Image loading error")
self.size = image.size
pixels = np.asarray(image, dtype=np.uint8)
self.image_bits = np.unpackbits(pixels)
def save(self,file_name):
"""Save image to file with current pixel data (as 'image_bits')
"""
packed_bits = np.packbits(self.image_bits)
packed_bits_reshaped = np.reshape(packed_bits,(self.size[1],self.size[0],3))
image = Image.fromarray(packed_bits_reshaped)
image.save(file_name)