-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlenscomparator.py
314 lines (274 loc) · 12.9 KB
/
lenscomparator.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
from PIL import Image, ImageColor, ExifTags, ImageFont, ImageDraw
from os.path import abspath, isfile, isdir, splitext, basename
from os import listdir
import datetime
import enum
import argparse
class Config:
# x, y in % of og image, marking the middle
CENTER_POS = (.5, .5) # center
MID_POS = (.7, .3) # upper right corner
CORNER_POS = (.95, .05) # upper right corner
CropWidth = 400
CropHeight = 300
# one lens, all positions
Merge = False
# final picture settings
Space = 10
Headliner = 200
Aperture_Space = 200
Default_BG = (224, 224, 224)
Other_BG = (200, 200, 200)
Mark = False
MarkOutline = (255, 0, 0)
class ImagePos(enum.Enum):
CENTER = 0
MID = 1
CORNER = 2
ALL = 255
def get_coord(self):
if self == self.CENTER:
return Config.CENTER_POS
elif self == self.MID:
return Config.MID_POS
elif self == self.CORNER:
return Config.CORNER_POS
def get_name(self):
names = {
self.CENTER: "Center",
self.MID: "Mid",
self.CORNER: "Corner"
}
return names[self]
class ImageMetadata:
def __init__(self, name, focal_length, aperture):
self.name = name
self.focal_length = focal_length
self.aperture = aperture
class ImageFragment:
def __init__(self, image: Image.Image, metadata: ImageMetadata, pos: ImagePos):
self.image = image
self.metadata = metadata
self.pos = pos
@staticmethod
def get_box(pos: ImagePos, img_w, img_h, crop_w, crop_h):
pos_coord = pos.get_coord()
center_w = img_w * pos_coord[0]
center_h = img_h * pos_coord[1]
# left, top, right, bottom lines
box = (center_w - crop_w/2, center_h - crop_h/2, center_w + crop_w/2, center_h + crop_h/2)
# adjusting in case it don't fit
# adjusting left
if box[0] < 0:
box = (0, box[1], box[2]-box[0], box[3])
# adjusting top
if box[1] < 0:
box = (box[0], 0, box[2], box[3] - box[1])
# adjusting right
if box[2] > img_w:
box = (box[0] - (box[2] - img_w), box[1], img_w, box[3])
# adjusting bottom
if box[3] > img_h:
box = (box[0], box[1] - (box[3] - img_h), box[2], img_h)
return box
@staticmethod
def pull_fragment(complete_image: Image.Image, pos, width, height, metadata: ImageMetadata):
box = ImageFragment.get_box(pos, complete_image.width, complete_image.height, width, height)
img = complete_image.crop(box)
return ImageFragment(img, metadata, pos)
class Comparison:
def __init__(self):
self.centers = []
self.mids = []
self.corners = []
def generate_comparison_image(self, pos: ImagePos, space, headliner, aperture_space):
if pos == ImagePos.CENTER:
working_set = self.centers
elif pos == ImagePos.MID:
working_set = self.mids
elif pos == ImagePos.CORNER:
working_set = self.corners
else:
working_set = self.centers + self.mids + self.corners
# get unique apertures, different models
#space = Config.Space # space between images, both in x and y
#headliner = Config.Headliner # space for lens name
#aperture_space = 200 # space for aperture text
apertures = dict()
lens_names = set()
max_w = 0
max_h = 0
for fragment in working_set:
name = "{}\n{}mm:\n{}".format(fragment.metadata.name, fragment.metadata.focal_length, fragment.pos.get_name())
if fragment.metadata.aperture not in apertures.keys():
apertures[fragment.metadata.aperture] = {name: fragment}
else:
apertures[fragment.metadata.aperture][name] = fragment
lens_names.add("{}{}".format(fragment.pos.value, name))
max_h = max(max_h, fragment.image.height)
max_w = max(max_w, fragment.image.width)
img_h = (max_h+space) * len(apertures.keys()) + headliner
img_w = (max_w+space) * len(lens_names) + aperture_space
comparisonImg = Image.new('RGB', (img_w, img_h), Config.Default_BG)
comparisonDraw = ImageDraw.Draw(comparisonImg)
# make sure order isn't fucked up!!!!
x_pos = 0
y_pos = 0
font = ImageFont.truetype("verdana.ttf", 16)
font_ap = ImageFont.truetype("verdana.ttf", 24)
x_pos += aperture_space + 15
y_pos += headliner/3
# sort lens_names so it's lens1 center, lens2 center, lens1 mid, lens2 mid, lens1 corn, lens2 corn...
# clever solution that just pasted the value we wanted first in front, so sorting algo works easily...
lens_names = sorted(lens_names)
# amd just quietly rids of the addition.
lens_names = [x[1:] for x in lens_names]
for name in lens_names:
# print columns (lens names)
comparisonDraw.text((x_pos, y_pos), name, fill=(0, 0, 0), font=font)
comparisonDraw.line((x_pos-15, 0, x_pos-15, headliner), fill=(0, 0, 0), width=5)
x_pos += max_w
# reset pos
x_pos = 0
y_pos = headliner
i = 0
for ap in sorted(apertures.keys()):
# paste in results
# alternate light grey and white backgrounds
if i % 2:
comparisonDraw.rectangle((0, y_pos, img_w, y_pos + max_h + space), fill=Config.Other_BG)
i += 1
# print aperture
x_pos_ap = x_pos + aperture_space/3
y_pos_ap = y_pos + max_h/2 - 8
fmt = "F{:.1f}" if ap > 1 else "F{:.2f}"
comparisonDraw.text((x_pos_ap, y_pos_ap), fmt.format(ap), fill=(0,0,0), font=font_ap)
# paste images
x_pos = aperture_space
for name in lens_names:
fragment = apertures[ap].get(name, None)
if fragment:
comparisonImg.paste(fragment.image, (x_pos, y_pos+int(space/2)))
x_pos += max_w
y_pos += max_h + space
x_pos = 0
return comparisonImg
def add_fragment(self, fragment: ImageFragment):
if fragment.pos == ImagePos.CENTER:
self.centers.append(fragment)
if fragment.pos == ImagePos.MID:
self.mids.append(fragment)
if fragment.pos == ImagePos.CORNER:
self.corners.append(fragment)
def mark_image(img: Image):
draw = ImageDraw.Draw(img)
center_box = ImageFragment.get_box(ImagePos.CENTER, img.width, img.height, Config.CropWidth, Config.CropWidth)
mid_box = ImageFragment.get_box(ImagePos.MID, img.width, img.height, Config.CropWidth, Config.CropWidth)
corner_box = ImageFragment.get_box(ImagePos.CORNER, img.width, img.height, Config.CropWidth, Config.CropWidth)
draw.rectangle(center_box, fill=None, outline=Config.MarkOutline, width=10)
draw.rectangle(mid_box, fill=None, outline=Config.MarkOutline, width=10)
draw.rectangle(corner_box, fill=None, outline=Config.MarkOutline, width=10)
return img
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description="Lens comparator app.")
parser.add_argument("folders",
nargs="+",
type=str,
help="Required, folder with comparison files. If there is no EXIF data in JPEG files, please use following naming format:\n"
"LENSNAME_FOCALLENGTH_APERTURE.JPG, where lensname is anything and may include focal length e.g. for zooms. \n"
"Focal length includes actual focal length the shot was shot at.\n"
"Aperture however is either an integer, or a float with one dot.\n"
"e.g. Pergear35f1.6_33_2.8.jpg")
parser.add_argument("-m", "--merge",
action="store_true",
help="If present, will merge all lens comparison across different fields (useful if testing one lens)")
parser.add_argument("-cp", "--centerpos",
nargs=1,
type=str,
default=["50,50"],
help="Override of default center comparison location at 50, 50%% of the image.\n"
"Format: xpos,ypos e.g. 50,50")
parser.add_argument("-mp", "--midpos",
nargs=1,
type=str,
default=["70,30"],
help="Override of default mid comparison location at 70, 30%% (upper right) of the image.\n"
"Format: xpos,ypos e.g. 25,75")
parser.add_argument("-kp", "--cornerpos",
nargs=1,
type=str,
default=["100,0"],
help="Override of default mid comparison location at 100, 0%% (far right) of the image.\n"
"Format: xpos,ypos e.g. 5,95")
parser.add_argument("-cw", "--cropwidth",
nargs=1,
type=int,
default=[400],
help="Tested crop width in px. Default: 400")
parser.add_argument("-ch", "--cropheight",
nargs=1,
type=int,
default=[300],
help="Tested crop height in px. Default: 300")
parser.add_argument("-mk", "--mark",
action="store_true",
help="Mark one of the images with tested spots.")
# add scale? e.g. take a bigger crop but scale it down to fit?
# that's dumb and not very pixel-peepery
args = parser.parse_args()
def str_to_pos(s:str):
xpos, ypos = s.split(",")
xpos = float(xpos)/100
ypos = float(ypos)/100
return xpos,ypos
def exif_metadata(filepath):
# experimental
img = Image.open(filepath)
exif = {
ExifTags.TAGS[k]: v
for k, v in img._getexif().items()
if k in ExifTags.TAGS
}
focal_length = int(exif["FocalLength"][0]/100)
aperture = float(exif["FNumber"][0]/100)
name = exif["LensMake"].rstrip("\x00") + " " + exif["LensModel"].rstrip("\x00")
return ImageMetadata(name, focal_length, aperture)
def metadata_from_file(filepath: str):
splitted = basename(filepath).split("_")
if len(splitted) < 3:
return exif_metadata(filepath)
else:
return ImageMetadata(splitted[0], int(splitted[1]), float(splitted[2].rsplit(".", 1)[0]))
Config.Merge = args.merge
Config.CENTER_POS = str_to_pos(args.centerpos[0])
Config.MID_POS = str_to_pos(args.midpos[0])
Config.CORNER_POS = str_to_pos(args.cornerpos[0])
Config.CropWidth = args.cropwidth[0]
Config.CropHeight = args.cropheight[0]
Config.Mark = args.mark
for folder in args.folders:
print("Folder: {}".format(folder))
filenames = listdir(folder)
files = ["{}\\{}".format(folder, x) for x in filenames]
comp = Comparison()
i = 1
files = list(filter(lambda x: "Comparison_" not in x and ("jpg" in x or "JPG" in x) and "_marked" not in x, files)) # ignore nonjpegs, marked and comparisons
for file in files:
print("{} [{}/{}]".format(file, i, len(files)))
meta = metadata_from_file(file)
image = Image.open(file)
comp.add_fragment(ImageFragment.pull_fragment(image, ImagePos.CENTER, Config.CropWidth, Config.CropHeight, meta))
comp.add_fragment(ImageFragment.pull_fragment(image, ImagePos.MID, Config.CropWidth, Config.CropHeight, meta))
comp.add_fragment(ImageFragment.pull_fragment(image, ImagePos.CORNER, Config.CropWidth, Config.CropHeight, meta))
if i == 1 and Config.Mark:
mark_image(image).save("{}_marked.JPG".format(file.rsplit(".", 1)[0]))
i += 1
if Config.Merge:
img = comp.generate_comparison_image(ImagePos.ALL, Config.Space, Config.Headliner, Config.Aperture_Space)
img.save("{}/Comparison_{}_all.JPG".format(folder, datetime.datetime.now().strftime("%Y-%m-%d_%H%M")), quality=100, subsampling=0)
else:
img = comp.generate_comparison_image(ImagePos.CENTER, Config.Space, Config.Headliner, Config.Aperture_Space)
img.save("{}/Comparison_{}_center.JPG".format(folder, datetime.datetime.now().strftime("%Y-%m-%d_%H%M")), quality=100, subsampling=0)
img = comp.generate_comparison_image(ImagePos.MID, Config.Space, Config.Headliner, Config.Aperture_Space)
img.save("{}/Comparison_{}_mid.JPG".format(folder, datetime.datetime.now().strftime("%Y-%m-%d_%H%M")), quality=100, subsampling=0)
img = comp.generate_comparison_image(ImagePos.CORNER, Config.Space, Config.Headliner, Config.Aperture_Space)
img.save("{}/Comparison_{}_corner.JPG".format(folder, datetime.datetime.now().strftime("%Y-%m-%d_%H%M")), quality=100, subsampling=0)