-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageProcessing.py
99 lines (76 loc) · 2.59 KB
/
imageProcessing.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
95
96
97
98
99
#-take middle few pixels
#-or all pixels
#-remove black/white pixels
import os.path
from PIL import Image
import csv
rootdirectory = 'E:\MapsProj'
for root,dirs,files in os.walk(rootdirectory):#insert filename here
print("\n")
print ("root: ",root)
print ("files: ",files)
print("directories: ", dirs)
file_to_open = root + '/' + "average.csv"
f = open(file_to_open,'w')
writer = csv.writer(f)
for f in range (0,len(files)):
if (files[f][-4:] == ".jpg"):
im = Image.open(root + "/" + files[f])
print("\n", im.format, im.size, im.mode)
npixels = im.size[0]*im.size[1]
cols = im.getcolors(npixels)
#print (cols)
#sumRGB = [(x[0]*x[1][0], x[0]*x[1][1], x[0]*x[1][2]) for x in cols]
sumRGB = []
for x in cols:
sumRGB.append((x[0]*x[1][0], x[0]*x[1][1], x[0]*x[1][2]))
avg = tuple([sum(x)/npixels for x in zip(*sumRGB)])
print (type(avg))
#write to csv
writer.writerow(avg)
#im.show()
# rootdirectory = 'D:\Documents\Maps'
rootCSV = rootdirectory + '/' + "average.csv"
## Open up a writer in root for writing csv in root
## Overriding newline as blank to remove blank entries written
with open(rootCSV, 'w') as rootF:
rootWriter = csv.writer(rootF)
rootWriter.writerow(['R','G','B','Lat','Lon'])
for root,dirs,files in os.walk(rootdirectory):#insert filename here
print("\n")
print ("root: ",root)
print ("files: ",files)
print("directories: ", dirs)
## Only open CSV that are not in rootdir
if (root != rootdirectory):
file_to_open = root + '/' + "average.csv"
print("Opening: " + file_to_open)
with open(file_to_open, 'r') as f:
reader = csv.reader(f)
colors = [0.0,0.0,0.0];
#initialize count for number of valid entries in csv
validRowsCount = 0
for row in reader:
if (len(row) != 0):
# print('Reading row:',end='')
print(row)
#Increment count for each entry
validRowsCount+=1
for index,color in enumerate(row):
colors[index] += float(color)
# get dir name from last part of path name
dirName = root.split("\\")[-1]
# split dir name into lat & lon
latLonList = dirName.split('-')
if (validRowsCount == 0):
rootWriter.writerow([0,0,0,latLonList[0],latLonList[1]])
continue;
for index,color in enumerate(colors):
colors[index] = float(color/validRowsCount)
# append 2 cols: lat & lon
for coor in latLonList:
colors.append(coor)
# write color, lat and lon
# print('Writing row:',end='')
print(colors)
rootWriter.writerow(colors)