-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunAll.py
More file actions
183 lines (119 loc) · 4.85 KB
/
runAll.py
File metadata and controls
183 lines (119 loc) · 4.85 KB
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
#################
# runAll.py
#################
import convertOGIntoSquare
import averageColor
import findClosestColor
import convertIntoFinalImage
import imageSetting
import os
import math
import sys
from PIL import Image
import argparse
#
# runs all the methods together
#
def main():
# parse the arguments given in the command line
parser = argparse.ArgumentParser()
req_grp = parser.add_argument_group(title='required arguments')
req_grp.add_argument("-i", "--input", required=True, help="original image that is being converted")
req_grp.add_argument("-o", "--output", required=True, help="file name for final image to be saved to")
args = parser.parse_args()
# set the variables accordingly
refImgName = args.input
newFileName = args.output
# the original reference image
referenceImg = ""
# make sure the OG image exists
try:
referenceImg = imageSetting.load_img(refImgName, "")
# if it doesn't exist, print error message
except:
print("\nERROR! Your file was not found:\t", refImgName)
exit()
# check the newFile doesn't already exist
for root, dirs, filenames in os.walk("."):
for f in filenames:
if newFileName == f:
print("\nERROR! This image already exists.")
print("Are you sure you want to override? (y/n)")
answer = input("\t>> ")
if answer == "n" or answer == "":
exit()
# the directory where original images are found
inDir = './originalPhotos/'
# directory where reduced photos will be saved
outDir = './reducedPhotos/'
print("\nThis program will be converting", refImgName + ".")
# used to name the images
i = 0
# resolution of the square images that make up the larger image
imageSize = 115.0
################################
# REDUCE IMAGES TO 115x115
################################
print("\n>> Reducing images...\n")
for root, dirs, filenames in os.walk(inDir):
for f in filenames:
if ('.jpg' in f or '.JPG' in f):
# convert the image into a square 100x100 and save that image
newImgName = \
convertOGIntoSquare.convertOGIntoSquare(f, inDir, outDir, i, imageSize)
print("\tConverted %s " %(newImgName))
# increment so that file names are different
i += 1
########################
# MAKE FINAL IMAGE
########################
print("\nDone reducing images. \n\n>> Matching pixels with images...")
# the directory where square 100x100 images are found
inDir = './reducedPhotos/'
# get the array of pixels for the image and its size
pixels = referenceImg.getdata()
width, height = referenceImg.size
# ARRAYS
new_pixel_image_arrays = [] # array containing image contents
phts_order = [] # photos in order for final image
avg_colors = [] # contains average
new_pixels = [] # final image array
# go through each reduced photo and add its average color and pixel array
# to 2 separate lists
for root, dirs, filenames in os.walk(inDir):
for f in filenames:
if ('.jpg' in f or '.JPG' in f):
# load the image
img = imageSetting.load_img(f, inDir)
# get the average color and add to list
avgColor = averageColor.averageColor(img)
avg_colors.append(avgColor)
# get the array of pixels for that image and add to list
pixelArray = img.getdata()
new_pixel_image_arrays.append(pixelArray)
# loops through each pixel in the reference image and finds the closest
# image for that pixel - adds this pixel array to the arrray of final
# images
for p in pixels:
colorInd = findClosestColor.findClosestColor(avg_colors, p)
phts_order.append(new_pixel_image_arrays[colorInd])
print("Done matching pixels with images.")
print("\n>> Creating final image...\n")
# sets the final array of pixels to the array that the function returns
new_pixels = \
convertIntoFinalImage.convertIntoFinalImage(phts_order, width, height, int(imageSize))
print("\nDone creating final image.")
# creating the image from the pixel array
print("\n>> Pulling data into jpg photo...")
newim = Image.new("RGB", (width * int(imageSize), height * int(imageSize)))
newim.putdata(new_pixels)
print("Done pulling data.")
# save image and show it
print("\n>> Saving image...")
imageSetting.save_img(newim, newFileName)
print("Done saving image.")
imageSetting.show_img(newim)
print("\n*** Done! Your new image has been created. ")
if __name__ == '__main__':
main()
#