-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.py
70 lines (51 loc) · 1.42 KB
/
file.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
from matplotlib import pyplot as plt
import numpy as np
# @time_this
def image_read(filename):
image = plt.imread(filename)
# already greyscale -> return as is
if image.ndim==2:
return image
# greyscale and alpha -> remove alpha
if image.shape[2]==2:
return image[...,1]
# rgb or rgba -> convert to greyscale
return rgb_to_greyscale(image[...,:3])
def rgb_to_greyscale(rgb_triplets):
return rgb_triplets @ (.299, .587, .114)
# @time_this
def image_write(filename, pixels):
plt.imsave(filename, pixels, cmap='gray')
def to255(image):
# return np.array(image*255, dtype=np.uint8)
return np.array(image*255, dtype=np.int32)
def load(interface, filename):
interface.image = image_read(filename)
interface.update_image()
print(f'[image loaded] {filename}')
def save(filename):
...
# class img:
# counter = 1
# current = None
# def load(filename):
# # global current
# image = image_read(filename)
# image_write('workspace/current.png', image)
# img.current = image
# def save(filename=None):
# # global counter, current
# if not filename:
# filename = f'workspace/{img.counter}.png'
# img.counter += 1
# image_write(filename, img.current)
# global counter, current
# img.counter = 1
# img.current = None
# plt.figure('workspace')
# plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
# plt.axis('off')
# plt.imshow(image_read('834.png'), cmap='gray')
# a = input('>>> ')
# plt.show()
# a = input('>>> ')