-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageSource.py
86 lines (70 loc) · 2.51 KB
/
imageSource.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
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image # Python Imaging Library https://pypi.org/project/Pillow/
class ImageSource:
def __init__(self):
self.pixel_seq = None
self.img_path = None
self.img = None
self.mode = None
self.num_of_channels = None
self.width = None
self.height = None
self.bitmap = None
self.channels = None
def load_from_file(self, img_path):
"""Loads the image from a file via PIL
Arguments:
img_path {str} -- Absolute path to the image file
Returns:
self -- Returns self to do the following: ImageSource().load_from_file()
"""
self.img_path = img_path
img = Image.open(self.img_path)
self.img = img
self.mode = img.mode
self.num_of_channels = len(img.getbands())
self.width = img.width
self.height = img.height
# "bitmap" is a height x width x num_of_channels numpy array
self.bitmap = np.array(img).astype('uint8')
if self.num_of_channels > 1:
self.channels = [self.bitmap[:, :, i]
for i in range(self.num_of_channels)]
else:
self.channels = [self.bitmap]
return self
def get_bitmap(self):
return self.bitmap
def get_pixel_seq(self):
if self.pixel_seq is None:
self.pixel_seq = self.bitmap.ravel()
return self.pixel_seq
def to_bitmap(self, pixel_seq=None):
if pixel_seq is None:
pixel_seq = self.get_pixel_seq()
return pixel_seq.reshape((self.height, self.width, self.num_of_channels))
def show(self):
self.img.show()
def show_color_hist(self):
colors = [(1, 0, 0, 0.5), (0, 1, 0, 0.5), (0, 0, 1, 0.5)]
for i, channel in enumerate(self.channels):
plt.hist(channel.ravel(), bins=range(255), fc=colors[i])
plt.show()
def _clear(self):
self.bitmap = None
self.img = None
self.pixel_seq = None
self.img_path = None
def from_bitmap(self, bitmap):
self._clear()
self.pixel_seq = bitmap.ravel()
self.bitmap = self.pixel_seq.reshape((self.height, self.width, self.num_of_channels))
self.img = Image.fromarray(self.bitmap, self.mode)
self.img_path = "bitmap"
def __str__(self):
return f"""
Location: {self.img_path}
Mode: {self.mode}
Size: {self.width} x {self.height}
"""