-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigital_images.py
45 lines (34 loc) · 1.06 KB
/
digital_images.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
import numpy as np
from math import pow
def to_greyscale(channels: list, weights: list = None):
dimensions = np.array(channels[1]).shape
greyscale = np.zeros(dimensions)
if weights is None:
weights = [1 / len(channels)] * len(channels)
for channel, weight in zip(channels, weights):
channel = np.array(channel)
if channel.shape != dimensions:
raise Exception('Channel dimensions must match')
greyscale += weight * channel
return greyscale
def quantize(image, desired: int, current: int = 255):
factor = (1 / current) * pow(2, desired)
return np.floor(factor * image)
def main():
red_channel = np.array([
[205, 195],
[238, 203]
])
green_channel = np.array([
[143, 138],
[166, 143]
])
blue_channel = np.array([
[154, 145],
[174, 151]
])
greyscale = np.around(to_greyscale([red_channel, green_channel, blue_channel]), 0)
print(quantize(greyscale, 8))
print(quantize(greyscale, 2))
if __name__ == "__main__":
main()