-
Notifications
You must be signed in to change notification settings - Fork 16
/
resizeImage_vr01.py
94 lines (76 loc) · 3.47 KB
/
resizeImage_vr01.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
87
88
89
90
91
92
93
94
import matplotlib.pyplot as plt
from skimage.color import rgba2rgb
from skimage.transform import resize
from skimage import io, img_as_ubyte
import os
import psutil
import shutil
def resize_all_images_to_8x8():
for dirpath , _ , filenames in os.walk("."):
for f in filenames:
if f.endswith(".jpg") or f.endswith(".jpeg") or f.endswith(".png"):
# print(os.path.abspath(os.path.join(dirpath, f)))
inputImage = os.path.abspath(os.path.join(dirpath, f))
fac = None
mode = 'constant'
img = io.imread(inputImage, plugin="matplotlib")
print(img.shape)
plt.imshow(img)
plt.show()
#mode='constant', cval=0, clip=True, preserve_range=False, anti_aliasing=True, anti_aliasing_sigma=None
if img.shape[-1] == 4:
img_resized = resize( rgba2rgb(img),
(8,8),
mode=mode,
cval=0,
clip=True,
preserve_range=False,
anti_aliasing=False,
anti_aliasing_sigma=fac )
else:
img_resized = resize( img,
(8,8),
mode=mode,
cval=0,
clip=True,
preserve_range=False,
anti_aliasing=False,
anti_aliasing_sigma=fac )
plt.imshow(img_resized)
plt.show()
img_resized_binary = img_as_ubyte( img_resized )
print(img_resized_binary.shape)
if f.endswith(".jpeg"):
outputImage = f[:-5]
else:
outputImage = f[:-4]
with open(outputImage+".data","wb") as f:
f.write(bytes(img_resized_binary))
def list_data_files():
data_files = list()
for dirpath , _ , filenames in os.walk("."):
for f in filenames:
if f.endswith(".data"):
# print(os.path.abspath(os.path.join(dirpath, f)))
data_files.append(os.path.abspath(os.path.join(dirpath, f)))
return data_files
def get_partition():
allParitions = psutil.disk_partitions()
if os.name == 'nt':
for partition in allParitions:
if 'removable' in partition.opts:
return partition.device
else:
for partition in allParitions:
if 'CIRCUITPY' in partition.mountpoint:
return partition.mountpoint
raise ValueError('could not find partition')
def copy_data_files_to_board():
data_files = list_data_files()
path = get_partition()
for dir_file in data_files:
dst = os.path.join(path, os.path.basename(dir_file))
shutil.copyfile(dir_file, dst)
print("copy %s %s" % (dir_file, dst))
resize_all_images_to_8x8()
copy_data_files_to_board()