-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgManip.py
248 lines (190 loc) · 8.34 KB
/
imgManip.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from PIL import Image
import time
# Timer decorator
def timeit(f):
def helper(*args, **kwargs):
start = time.time()
to_return = f(*args, **kwargs)
print('{} seconds'.format(time.time() - start))
return to_return
return helper
loadpath = '/images/'
savepath = '/output/'
# Function to compare and get the image with the brightest or darkest pixels out of the input values
@timeit
def pixelComp(file1="pic1.JPG", file2="pic2.JPG", bright=True, filename="output.JPG"):
try:
# Open images into program
img1 = Image.open(file1)
img2 = Image.open(file2)
# Get pixels from images
im1 = img1.load()
im2 = img2.load()
width, height = img1.size
# Initialize final image
finalImg = Image.new('RGB', (width, height))
finalPixels = finalImg.load()
# Iterate and compare to find brightest or darkest pixel
for i in range(width):
for j in range(height):
# Convert RGB to luminosity measurement
lum1 = calcLum(im1[i, j])
lum2 = calcLum(im2[i, j])
# Compare brightness of pixels
# Changed from direct comparison of pixels
# Compare, want to be true if brighter when night, darker when day etc
if (lum1 > lum2) == bright:
finalPixels[i, j] = im1[i, j]
else:
finalPixels[i, j] = im2[i, j]
finalImg.save(filename) # show()
print("completed: ", filename)
except IOError:
print("It broke")
raise(IOError)
# Function to compare and get the brightest or darkest image of the two
def imageComp(file1="pic1.JPG", file2="pic2.JPG", bright=True, filename="output.JPG"):
try:
# Open images into program
img1 = Image.open(file1)
img2 = Image.open(file2)
# Get pixels from images
im1 = img1.load()
im2 = img2.load()
width, height = img1.size
# Initialize luminosity sum for overall brightness
lum1 = 0
lum2 = 0
# Iterate and compare to find brightest or darkest pixel
for i in range(width):
for j in range(height):
# Convert RGB to perceived luminosity measurement
lum1 += calcLum(im1[i, j])
lum2 += calcLum(im2[i, j])
# See which image has the greatest total luminosity
if (lum1 > lum2) == bright:
img1.save(filename) # show()
else:
img2.save(filename)
print("completed")
except IOError:
print("It broke")
pass
# Def color shift prototype to see the dealio
@timeit
def colorShift(file="pic1.jpg", shift=5):
# image shift add pixel
# iterate thru image and set value of pixel xyz to xyz+5 for r, and mins for b
# Function to get the image with the greatest variety/range of colors
# Maybe use numpy for the image manipulations instead? Might be moer efficient for the shifting, much faster as well
try:
# Open images into program
img = Image.open("Image\\" + file)
print("Loaded image")
# Get pixels from images
im = img.load()
width, height = img.size
print("Height: ", height, "\nWidth: ", width)
# Output image
finalImg = Image.new('RGB', (width, height))
finalPixels = finalImg.load()
print(type(im))
for i in range(width):
# Increment K here, or after the below loop
for j in range(height):
# Store the R values here, as
# values[k][j], _, _= im[i,j]
# Then store the final pixels as however
# Convert the pixels into smaller gaps, so close colors are
# not double counted, to try to get a larger difference in color
# r1, g1, b1 = (im[i, j])
r, g, b = (im[i, j])
r2, g2, b2 = finalPixels[i, j]
# Shift along the width
iShiftPlus = min((i+shift) % width, i+shift)
iShiftMinus = max((i-shift) % width, i-shift)
# This is done incorrectly, we need to access the old data from each new place we place it, the current data in the final image and all other data
# So we don't override it, but this effect is cool.
# Assign New Photo image
finalPixels[i, j] = r2, b, g2
finalPixels[iShiftPlus, j] = r2, b2, g
finalPixels[iShiftMinus, j] = r, b2, g2
finalImg.save("Output\\output_" + str(shift) + file)
# finalImg.save("output.jpg")
except IOError:
print("It broke")
pass
# Function to get the image with the greatest variety/range of colors
def colorComp(file1="pic1.JPG", file2="pic2.jpg", filename="output.jpg"):
# Initialize the RGB list for large sizes of lists
# rgbRange = [[[0 for i in range(32)], [0 for j in range(32)], [0 for k in range(32)]] for x in range(2)]
# Create two lists to store the range of pixels with flags of 1 being present, 0 else
# Done incorrectly, aslso make it groups of 16, and nested
rgbRange1 = [[[0 for i in range(16)] for j in range(16)] for k in range(16)]
rgbRange2 = [[[0 for i in range(16)] for j in range(16)] for k in range(16)] # [[0 for i in range(32)], [0 for j in range(32)], [0 for k in range(32)]]
try:
# Open images into program
img1 = Image.open(file1)
img2 = Image.open(file2)
# Get pixels from images
im1 = img1.load()
im2 = img2.load()
width, height = img1.size
for i in range(width):
for j in range(height):
# Convert the pixels into smaller gaps, so close colors are
# not double counted, to try to get a larger difference in color
r1, g1, b1 = (im1[i, j])
r1 = r1 // 16
b1 = b1 // 16
g1 = g1 // 16
r2, g2, b2 = (im2[i, j])
r2 = r2 // 16
b2 = b2 // 16
g2 = g2 // 16
rgbRange1[r1][g1][b1] = 1
rgbRange2[r2][g2][b2] = 1
# Iterate through the rgb list to see which has a larger number
rgbSum1 = 0
rgbSum2 = 0
for i in range(16):
for j in range(16):
for k in range(16):
rgbSum1 += rgbRange1[i][j][k]
rgbSum2 += rgbRange2[i][j][k]
if rgbSum1 > rgbSum2:
img1.save(filename)
else:
img2.save(filename)
print("completed")
except IOError:
print("It broke")
pass
# Function to calculate luminosity based upon human sight perception
def calcLum(color):
red, green, blue = color
return ((red * 0.2126 + green * 0.7152 + blue * 0.0722) / 255)
if __name__ == '__main__':
# pixelComp(file1="Image\\brightest\\DSC_0454.JPG",
# file2="Image\\brightest\\DSC_0455.JPG", bright=False, filename="darkest1.JPG")
# pixelComp(file1="Image\\brightest\\DSC_0456.JPG",
# file2="Image\\brightest\\DSC_0457.JPG", bright=False, filename="darkest2.JPG")
# pixelComp(file1="Image\\brightest\\DSC_0458.JPG",
# file2="Image\\brightest\\DSC_0459.JPG", bright=False, filename="darkest3.JPG")
# pixelComp(file1="Image\\brightest\\DSC_0460.JPG",
# file2="Image\\brightest\\DSC_0461.JPG", bright=False, filename="darkest4.JPG")
# pixelComp(file1="darkest1.JPG",
# file2="darkest2.JPG", bright=False, filename="darkest5.JPG")
# pixelComp(file1="darkest4.JPG",
# file2="darkest3.JPG", bright=False, filename="darkest6.JPG")
# pixelComp(file1="darkest5.JPG",
# file2="darkest3.JPG", bright=False, filename="darkest7.JPG")
# pixelComp(file1="darkest5.JPG",
# file2="darkest6.JPG", bright=False, filename="darkest_final.JPG")
# colorComp(file1="img1.JPG", file2="img2.JPG", filename="darkest.JPG")
start_time = time.time()
for i in range(0,11):
# run_time = time.time()
colorShift("DSC_0063.jpg", shift=25*i)
# print("Run length with " + str(i) + " shift: ", time.time() - run_time)
print("Total Time Elapsed: ", time.time() - start_time)